17

MY XML FILE STRUCTURE

<items>
  <item>
    <itemID>1</itemID>
    <isGadget>True</isGadget>
    <name>Star Wars Figures</name>
    <text1>LukeSkywalker</text1>
  </item>
</items>

TO READ DATA FROM XML BY ITEMID

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 new
            {
                itemID = item.Element("itemID").Value,
                isGadget = bool.Parse(item.Element("isGadget").Value),
                name = item.Element("name").Value,
                text1 = item.Element("text1").Value,
             }

foreach (var item in items)
{
     ....
}

How to update XML data by itemID? Thanks!

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • 1
    I dont understand why theses LINQ-to-XML questions are so pervasive at the moment. The answers are _right here_. http://msdn.microsoft.com/en-us/library/bb387087.aspx – Gusdor Oct 03 '13 at 07:45

3 Answers3

19

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");
Canavar
  • 47,715
  • 17
  • 91
  • 122
  • Not working. Maybe I'm missing something. Do I have to add xmlDoc.Save("data.xml")? Even with xmlDoc.Save, it still does not update. –  Sep 29 '09 at 08:17
  • Canavar, I was hoping to update the xml file directly instead of having to convert it into a string first. thanks! –  Oct 03 '09 at 01:16
9

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) { .... } 
Rajamohan Anguchamy
  • 1,726
  • 1
  • 17
  • 35
4

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();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Can you provide a simple example of call toList() to update xml? Do I need to use the foreach loop to setElementValue? –  Sep 29 '09 at 04:23
  • 1
    Calling ToList() doesn't update the XML, and yes, you'd need to use `foreach`. You haven't said how you want to update the XML though, so it's somewhat tricky to give an example. – Jon Skeet Sep 29 '09 at 05:19
  • Not sure what are the options for updating a XML document. Maybe you can suggest the most efficient method. :) –  Sep 29 '09 at 08:12
  • Can we really recommend calls to ToList? I've seen junior developers get it into their heads that you need a ToList or ToArray after every single query regardless of usage. It seems to me there should be better usage guidelines. This is after all, the gateway to using List.ForEach(). Grim! – Gusdor Oct 03 '13 at 07:49