我正在尝试向看起来基本相似的现有特定 xml 文档添加条目:
<body>
<setting>
<app name="notepad.exe" folder= "c:\windows\system32\" owner="peter"/>
<app name="calc.exe" folder= "c:\windows\system32\" age="4"/>
</setting>
</body>
我遇到的问题是我想添加多个应用程序条目,每个条目都有自己的宽度,例如所有者或年龄等(ea 不会覆盖单个条目)。
我在想
XDocument doc = new XDocument();
System.xml.XmlElement appnew = new system.Xml.XmlElement("<app name=\"write.exe\" folder="\c:\\windows\\system32\\\"")
然后将 XmlElement 添加到 sttings 部分,但是 XMLElement 不能那样设置,所以我想知道如何添加相等的节点名称?
最佳答案
尝试以下
var doc =
new XDocument(
new XElement("body",
new XElement("setting",
new XElement("app", new XAttribute("age", "4")),
new XElement("app", new XAttribute("owner", "bitchiko"))
)));
如果您有现有 XDocument
并尝试添加 新的app
元素。然后尝试以下
var doc= new XDocument(...);
var settingsXElement = doc.Descendants("setting").Single();
settingsXElement.Add(new XElement("app", new XAttribute("owner", "tchelidze")));
关于c# - 添加 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320528/