XML File Update (C # / Linq)

I am trying to figure out how I can update my xml file. I know how to read and write, but I do not know how to update an existing record.

My xml file looks like this:

And I would like to be able to change the XAttribute value that is already in the file.

This is how I write the file:

XElement xElement; xElement = new XElement("Orders"); XElement element = new XElement( "Order", new XAttribute("Quantity", Quantity), new XAttribute("Part No", PartNo), new XAttribute("Description", Description), new XAttribute("Discount", Discount), new XAttribute("Freight", Freight), new XAttribute("Unit Value", UnitValue), new XAttribute("Line Total", LineTotal) ); xElement.Add(element); xElement.Save(""); 

Is it possible to make updates, or should we first delete the existing one and then re-add it with the new values?

+4
source share
1 answer

Yes, you can update the attribute without deleting and re-adding. Just get the desired XAttribute inside the XElement and update its Value property and save the XElement to a file to see the changes.

 xElement.Attribute("Quantity").Value = "15"; 
+5
source

All Articles