tags:

views:

352

answers:

4

Hi, What is the best way to update an XElement (update the value of itemNumber of element Pen) in this XML?

<?xml version="1.0" encoding="utf-8"?>
<MyStore>
  <Category>
    <itemName>Pen</itemName>
    <itemNumber>12</itemNumber>
  </Category>
  <Category>
    <itemName>Paper</itemName>
    <itemNumber>23</itemNumber>
  </Category>
</MyStore>
+1  A: 

I would use Xpath to find the element you were looking for, then manipulate it directly.

For the XPath something like //Category[itemName='Pen']/itemNumber would find the element for more on Xpath see

The following (very basic) snippet works for me in mono

using System.Xml;

namespace test
{

  class myclass
  {

  public static void Main(string[] argv)
  {
    XmlTextReader reader = new XmlTextReader(argv[0]);
    XmlDocument doc = new XmlDocument(); 
    doc.Load(reader);
    reader.Close();          
    XmlNode myNode;
    XmlElement root = doc.DocumentElement;
    myNode = root.SelectSingleNode("//Category[itemName='Pen']/itemNumber");

    myNode.InnerText = "18";

    doc.Save(argv[1]);
  }
}
}
Andrew Cox
A: 

You could find it and update it using LinqToXml:

XElement root = XElement.Load("myXml.xml");

var penCategory = from category in root.Descendants("Category")
                  where category.Element("itemName") != null 
                     && category.Element("itemName").Value == "Pen"
                  select category;

penCategory.Element("itemName").Value = updatedValue;
womp
Why do you use `Descendants()` so aggressively, where `Elements()` would do just fine?
Pavel Minaev
Habit more than anything.
womp
+4  A: 
XDocument doc;
...
XElement penItemValue = doc
     .Elements("MyStore")
     .Elements("Category")
     .Elements("itemName")
     .Single(itemName => itemName.Value == "Pen")
     .Parent
     .Element("itemValue");
penItemValue.Value = "123";
Pavel Minaev
I have a problem with Elements and Element. When it was Elements, it gave me a null result for the above xml, but I have followed your approach and got it.
paradisonoir
A: 

Using XPath (which will be familiar to most XML devs):

var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
  <MyStore>
    <Category>
      <itemName>Pen</itemName>
      <itemNumber>12</itemNumber>
    </Category>
    <Category>
      <itemName>Paper</itemName>
      <itemNumber>23</itemNumber>
    </Category>
  </MyStore>";

  var doc = new XmlDocument();
  doc.LoadXml(xml);
  var nav = doc.CreateNavigator();
  var iter = nav.Select("/MyStore/Category[itemName='Pen']/itemNumber");
  iter.MoveNext();
  iter.Current.SetValue("42");
steamer25
He specifically asked about doing this for `XElement`.
Pavel Minaev