我的linq代码格式如下:
<Deck>
<Treasure>
<card>
.....
</card>
......
</treasure>
<Door>
<card>
.....
</card>
......
</Door>
在下面的代码中,如何添加与宝藏相同“级别”的另一个门?我尝试过的所有内容都会将其添加为与卡片相同的级别。这就是我所拥有的:
public void SaveXml(string path)
{
XElement xml;
XElement root = new XElement("Treasure");
foreach (var item in TreasureCards)
{
xml = new XElement("Card",
new XAttribute("name", item.Name),
new XElement("Type", item.Type),
new XElement("Image",
new XAttribute("path", item.Image)),
new XElement("Usage", item.Usage),
new XElement("Quantity", item.Quantity),
new XElement("Sell", item.Sell)
);
root.Add(xml);
}
root.Add(new XElement("Door"));
foreach (var item in DoorCards)
{
xml = new XElement("Card",
new XAttribute("name", item.Name),
new XElement("Type", item.Type),
new XElement("Image",
new XAttribute("path", item.Image)),
new XElement("Usage", item.Usage),
new XElement("Quantity", item.Quantity));
root.Add(xml);
}
答案 0 :(得分:1)
您需要先创建Deck元素:
XElement deck = new XElement("Deck");
然后添加宝藏(我已经冒充了从root重命名的宝藏)和它的大门:
XElement treasure = new XElement("Treasure")
...
deck.Add(treasure)
...
XElement door = new XElement("Door")
...
deck.Add(door)