因此,讓我們假設這就是我想要實現:的XElement =>在運行時添加子節點
<root>
<name>AAAA</name>
<last>BBBB</last>
<children>
<child>
<name>XXX</name>
<last>TTT</last>
</child>
<child>
<name>OOO</name>
<last>PPP</last>
</child>
</children>
</root>
不知道使用的XElement是最簡單的方法
,但是這是我有這麼遠:
XElement x = new XElement("root",
new XElement("name", "AAA"),
new XElement("last", "BBB"));
現在我必須添加「兒童」根據我有一些數據。
有可能是1,2,3,4 ......
,所以我需要遍歷經過我的列表,讓每一個孩子
foreach (Children c in family)
{
x.Add(new XElement("child",
new XElement("name", "XXX"),
new XElement("last", "TTT"));
}
問題:
這樣做的方式我會錯過「CHILDREN Parent node」。 如果我只是在foreach之前添加它,它會被渲染成一個封閉的節點
<children/>
這不是我們想要的。
問題:
我如何可以添加到第1部分父節點和我的列表中有許多?