Adding elements to an xml file in C#

前端 未结 7 1093
名媛妹妹
名媛妹妹 2020-11-27 19:00

I have an XML file formatted like this:


  
    
      testcode1
    
  &l         


        
7条回答
  •  醉梦人生
    2020-11-27 19:37

    You're close, but you want name to be an XAttribute rather than XElement:

     XDocument doc = XDocument.Load(spath); 
     XElement root = new XElement("Snippet"); 
     root.Add(new XAttribute("name", "name goes here")); 
     root.Add(new XElement("SnippetCode", "SnippetCode")); 
     doc.Element("Snippets").Add(root); 
     doc.Save(spath); 
    

提交回复
热议问题