I have an XML file with this structure:
<inventory>
<product>
<recordNumber>1</recordNumber>
<name>Dustbin</name>
<stock>190</stock>
<price>33</price>
</product>
<product>
<recordNumber>2</recordNumber>
<name>Broom</name>
<stock>200</stock>
<price>76</price>
</product>
</inventory>
I have a program which takes in these values and creates a List of 'Item' objects called 'stockArray'. each item has an int-Id(from recordNumber) and an int-count(from stock).
The program updates the "stock" value accordingly and what I am looking to do is then update the XML file to have the new 'stock' value.
I am very new to C# and XML, so this is the path I have been going down:
XDocument doc = XDocument.Load(xml);
var list = doc.Element("inventory").Elements("product");
foreach (var node in list)
{
foreach (Item item in stockArray)
{
if (node.Element("recordNumber").Value == Convert.ToString(item.Id))
node.SetElementValue("count", Convert.ToString(item.count));
}
}
But so far this seems way off course. I can find a lot of information about adding new nodes to an XML but iterating through the stockArray item list and updating the XML seems to be a different process.
Any advice would be greatly appreciated.
doc.Save(myPathToTheXmlFile)?<count>element, did you meanstock:node.SetElementValue("stock", Convert.ToString(item.count));?