我有以下xml:
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB CCC="it is aatr 1" DDD="it is attr 2">
</AAA>
我希望在我的c#应用程序中添加几个BBB节点,而不是我的xml将如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB CCC="it is aatr 1" DDD="it is attr 2">
<BBB CCC="it is another attr 1" DDD="it is another attr 2">
<BBB CCC="this is something else 1" DDD="this is something else 1">
</AAA>
任何代码建议?
答案 0 :(得分:3)
您可以使用DOM
(System.Xml)和Linq to XML
api。
Linq to XML
XDocument doc = XDocument.Load(file);
XElement ele = new XElement("BBB");
ele.SetAttributeValue("CCC", "text1");
ele.SetAttributeValue("DDD", "text2");
doc.Root.Add(ele);
doc.Save(file);