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>
XDocument doc;
...
XElement penItemValue = doc
.Elements("MyStore")
.Elements("Category")
.Elements("itemName")
.Single(itemName => itemName.Value == "Pen")
.Parent
.Element("itemValue");
penItemValue.Value = "123";
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With