gpt4 book ai didi

c# - 使用递归从嵌套对象到 XML 文档

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:40 25 4
gpt4 key购买 nike

我知道所提供的建议 here

但尽管如此,我还是希望得到一些帮助,以了解我的代码中的错误。

我有一个从这个类派生的对象列表

public class Leaf
{
public String key { get; set; }
public String id { get; set; }
public String value { get; set; }

public List<Leaf> branch { get; set; } //children

public Leaf()
{
branch = new List<Leaf>();
key = "";
id = "";
value = "";
parent_id_value = "";
}

}

主要我有这些对象:

List<Leaf> tree = new List<Leaf>();
XElement xmloutput = new XElement("root");

现在我想遍历我的列表及其子列表,以便使用以下规则创建嵌套的 XML 结构:

  1. 当 Leaf 对象没有子对象时,过程必须创建一个 <dictionaryitem/>元素
  2. 当 Leaf 有 child 时,过程必须创建一个 <dictionarygroup></dictionarygroup> ,其中包含子项,这些子项可以是 dictionaryitem 也可以是 dictionarygroup,具体取决于它们自己是否有子项。
  3. dictionaryitem 和 dictionarygroup 具有相同的属性,从 Leaf 对象派生。

我的递归过程如下:

public static XElement CreateString(List<Leaf> tree, XElement xmloutput)
{
XElement xml = null;
foreach(Leaf lf in tree)
{
if(lf.branch.Count > 0 && lf.branch[0].id!="")
{

xml = new XElement("dictionarygroup",
new XAttribute("codeScheme", 1),
new XAttribute("codeValue", lf.id),
new XAttribute("codeMeaning", lf.value),
new XAttribute("codeSchemeVersion", "01"),
new XAttribute("isCancelled", "false"),
new XElement(CreateString(lf.branch, null))
);

}
else
{
xml = new XElement("dictionaryItem",
new XAttribute("codeScheme", 1),
new XAttribute("codeValue", lf.id),
new XAttribute("codeMeaning", lf.value),
new XAttribute("codeSchemeVersion", "01"),
new XAttribute("isCancelled", "false")
);
}

if (xmloutput != null)
xmloutput.Add(xml);
else
xmloutput = xml;

xml = null;
}

return xmloutput;
}

它产生了一个结果,但不是预期的结果;让我们以这个数据为例:

List<Leaf> tree = new List<Leaf>();

Leaf leaf1 = new Leaf();
leaf1.key = "L1";
leaf1.id = "257170";
leaf1.value = "house";

Leaf leaf2 = new Leaf();
leaf2.key = "L1";
leaf2.id = "44444";
leaf2.value = "mouse";

Leaf leaf1_1 = new Leaf();
leaf1_1.key = "L2";
leaf1_1.id = "323233";
leaf1_1.value = "window";

Leaf leaf1_2 = new Leaf();
leaf1_2.key = "L2";
leaf1_2.id = "666666";
leaf1_2.value = "door";

leaf1.branch.Add(leaf1_1);
leaf1.branch.Add(leaf1_2);

tree.Add(leaf1);
tree.Add(leaf2);

我希望得到以下结果:

<root>
<dictionarygroup codeScheme="1" codeValue="257170" codeMeaning="house" codeSchemeVersion="01" isCancelled="false">
<dictionaryItem codeScheme="1" codeValue="323233" codeMeaning="window" codeSchemeVersion="01" isCancelled="false" />
<dictionaryItem codeScheme="1" codeValue="666666" codeMeaning="door" codeSchemeVersion="01" isCancelled="false" />
</dictionarygroup>
<dictionaryItem codeScheme="1" codeValue="44444" codeMeaning="mouse" codeSchemeVersion="01" isCancelled="false" />

相反,我得到了这个:

<root>
<dictionarygroup codeScheme="1" codeValue="257170" codeMeaning="house" codeSchemeVersion="01" isCancelled="false">
<dictionaryItem codeScheme="1" codeValue="323233" codeMeaning="window" codeSchemeVersion="01" isCancelled="false">
<dictionaryItem codeScheme="1" codeValue="666666" codeMeaning="door" codeSchemeVersion="01" isCancelled="false" />
</dictionaryItem>
</dictionarygroup>
<dictionaryItem codeScheme="1" codeValue="44444" codeMeaning="mouse" codeSchemeVersion="01" isCancelled="false" />

所以每次有 child 时,第一个被用作包含其他 sibling 的分组标签,所以它就像 parent 一样。

最佳答案

我设法用最少的更改修复了代码。

数据初始化部分:

List<Leaf> tree = new List<Leaf>();

Leaf leaf1 = new Leaf();
leaf1.key = "L1";
leaf1.id = "257170";
leaf1.value = "house";

Leaf leaf2 = new Leaf();
leaf2.key = "L1";
leaf2.id = "44444";
leaf2.value = "mouse";

Leaf leaf1_1 = new Leaf();
leaf1_1.key = "L2";
leaf1_1.id = "323233";
leaf1_1.value = "window";

Leaf leaf1_2 = new Leaf();
leaf1_2.key = "L2";
leaf1_2.id = "666666";
leaf1_2.value = "door";

leaf1.branch.Add(leaf1_1);
leaf1.branch.Add(leaf1_2);

tree.Add(leaf1);
tree.Add(leaf2);

//Create root element
XElement parentElement = new XElement("Root");

var result = CreateCorrectString(tree, parentElement);

CreateString 的实现:

public static XElement CreateCorrectString(List<Leaf> tree, XElement parent)
{
XElement xml = null;
foreach (Leaf lf in tree)
{
if (lf.branch.Count > 0 && lf.branch[0].id != "")
{
xml = new XElement("dictionarygroup",
new XAttribute("codeScheme", 1),
new XAttribute("codeValue", lf.id),
new XAttribute("codeMeaning", lf.value),
new XAttribute("codeSchemeVersion", "01"),
new XAttribute("isCancelled", "false")
);

CreateCorrectString(lf.branch, xml);
}
else
{
xml = new XElement("dictionaryItem",
new XAttribute("codeScheme", 1),
new XAttribute("codeValue", lf.id),
new XAttribute("codeMeaning", lf.value),
new XAttribute("codeSchemeVersion", "01"),
new XAttribute("isCancelled", "false")
);

}

parent.Add(xml);

}

return parent;
}

附言当然,这不是指定任务的最佳算法,但我尽量少改动源代码。

关于c# - 使用递归从嵌套对象到 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48746886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com