添加属性更改的节点

时间:2012-02-06 08:52:46

标签: c# xml

我有以下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>

任何代码建议?

1 个答案:

答案 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);