add data to existing xml file using linq

前端 未结 1 748
时光说笑
时光说笑 2020-12-16 20:27

I am a .net beginner. I need to add some data to xml file

the xml file is:

    --- 1st level  /* i dont want to create this because this         


        
相关标签:
1条回答
  • 2020-12-16 21:15

    Assuming you have the original document:

     var doc = XDocument.Load(...);
    

    then create a new element (not a document)

      //XDocument doc = new XDocument(      
      //  new XElement("stock",  /* how to go inside existing "stock"? */
       var newElement =  new XElement("items", 
                          new XElement("productname", "Toothpaste"),
                          new XElement("brandname", "CloseUp"),
                          new XElement("quantity","16"),
                          new XElement("price","15"));
    

    And then insert it:

      doc.Element("stock").Add(newElement);
    
      doc.Save(....);
    
    0 讨论(0)
提交回复
热议问题