Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加XML元素_C#_Xml - Fatal编程技术网

C# 添加XML元素

C# 添加XML元素,c#,xml,C#,Xml,我正在尝试向现有的特定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> 我的问题是,我想

我正在尝试向现有的特定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\\\"")
XDocument doc=new XDocument();
System.xml.xmlement appnew=new System.xml.xmlement(“尝试以下内容

  var doc =
      new XDocument( 
        new XElement("body",
          new XElement("setting",
            new XElement("app", new XAttribute("age", "4")),
            new XElement("app", new XAttribute("owner", "bitchiko"))
     )));
var doc= new XDocument(...);
var settingsXElement = doc.Descendants("setting").Single();
settingsXElement.Add(new XElement("app", new XAttribute("owner", "tchelidze")));
如果您有现有
XDocument
并试图添加
应用程序
元素,请尝试以下操作

  var doc =
      new XDocument( 
        new XElement("body",
          new XElement("setting",
            new XElement("app", new XAttribute("age", "4")),
            new XElement("app", new XAttribute("owner", "bitchiko"))
     )));
var doc= new XDocument(...);
var settingsXElement = doc.Descendants("setting").Single();
settingsXElement.Add(new XElement("app", new XAttribute("owner", "tchelidze")));

这对一个新文档来说是可行的,但我想“添加”这样的条目。因为我有一个令人兴奋的Xdocument,很抱歉我应该写得更清楚,我会编辑一个bitAh问题。谢谢这正是我需要的,我在这里接近了它,但那一个已经锁定了它。Try:XElement setting=doc.subjections(“app”).FirstOrDefault();setting.add(新XElement(“app”,新对象[]{new XAttribute(“name”,“notepad.exe”),XAttribute(“folder”,“c:\windows\system32\”),XAttribute(“owner”,“peter”)});