Update XML with C# using Linq

前端 未结 3 1793
故里飘歌
故里飘歌 2020-12-01 12:45

MY XML FILE STRUCTURE


  
    1
    True
    Star Wars Fi         


        
相关标签:
3条回答
  • 2020-12-01 13:16

    To update your xml use SetElementValue method of the XElement :

    var items = from item in xmlDoc.Descendants("item")
        where item.Element("itemID").Value == itemID
        select item;
    
    foreach (XElement itemElement in items)
    {
        itemElement.SetElementValue("name", "Lord of the Rings Figures");
    }
    

    EDIT : Yes, I tried your example and it saves updated data to the file. Save your updated xml with Save method of the XDocument, here is the code that I tried :

    string xml = @"<items>
               <item>
                <itemID>1</itemID>
                <isGadget>True</isGadget>
                <name>Star Wars Figures</name>
                <text1>LukeSkywalker</text1>
               </item>
            </items>";
    
    XDocument xmlDoc = XDocument.Parse(xml);
    
    var items = from item in xmlDoc.Descendants("item")
                where item.Element("itemID").Value == "1"
                select item;
    
    foreach (XElement itemElement in items)
    {
        itemElement.SetElementValue("name", "Lord of the Rings Figures");
    }
    
    xmlDoc.Save("data.xml");
    
    0 讨论(0)
  • 2020-12-01 13:23

    Your query is projecting to an anonymous type. If you want to just modify the elements themselves, you want something like:

    var items = from item in xmlDoc.Descendants("item")
                where item.Element("itemID").Value == itemID
                select item;
    

    Otherwise known as:

    var items = xmlDoc.Descendants("item")
                      .Where(item => item.Element("itemID").Value == itemID);
    

    I suggest you call ToList() as well, so that the whole query is performed and the results stored in a list before you start modifying things:

    var items = xmlDoc.Descendants("item")
                      .Where(item => item.Element("itemID").Value == itemID)
                      .ToList();
    
    0 讨论(0)
  • 2020-12-01 13:25

    To update your xml use element method method of the XElement :

    XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml"));
    var items = (from item in xmlDoc.Descendants("item")
                where item.Element("itemID").Value == itemID
                select item).ToList();
             foreach (var item in items)
             {
                    item.Element("itemID").Value=NewValue;
                    bool.Parse(item.Element("isGadget").Value)=Newvalue;
                    item.Element("name").Value=Newvalue;
                    item.Element("text1").Value=Newvalue;
             }
    xmlDoc.Save(HttpContext.Current.Server.MapPath("data.xml"));
    

    or

    XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml"));
                 foreach (var item in (from item in xmlDoc.Descendants("item")
                    where item.Element("itemID").Value == itemID
                    select item).ToList())
                 {
                        item.Element("itemID").Value=NewValue;
                        bool.Parse(item.Element("isGadget").Value)=Newvalue;
                        item.Element("name").Value=Newvalue;
                        item.Element("text1").Value=Newvalue;
                 }
        xmlDoc.Save(HttpContext.Current.Server.MapPath("data.xml"));
    

    you get information form dynamic and update those changes in button click event means, first you checks the Page load following code is present

    if(!Page.IsPostBack) { .... } 
    
    0 讨论(0)
提交回复
热议问题