Modify XML existing content in C#

前端 未结 4 1300
青春惊慌失措
青春惊慌失措 2020-11-29 06:04

Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing Content with XmlNode SelectSingleNode(), node.ChildNode[?].InnerText = someting, etc.

4条回答
  •  情深已故
    2020-11-29 06:29

    Using LINQ to xml if you are using framework 3.5

    using System.Xml.Linq;
    
    XDocument xmlFile = XDocument.Load("books.xml"); 
    var query = from c in xmlFile.Elements("catalog").Elements("book")    
                select c; 
    foreach (XElement book in query) 
    {
        book.Attribute("attr1").Value = "MyNewValue";
    }
    xmlFile.Save("books.xml");
    

提交回复
热议问题