Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate XAttribute Value where XAttribute Name = X
    text
    copied!<p>I have the following code which creates an XML file with a bunch of order information. And I'd like to be able to update an entry in this XML file, instead of deleting everything and re-adding everything again.</p> <p>I know I can do this:</p> <pre><code>xElement.Attribute(attribute).Value = value; </code></pre> <p>But that will change every attribute with the same name as attribute holds. How can I only change the value of something when the entry's Id equals "jason", for example? Would I need to Load the XML file, iterate over the entire file until it finds a match for the attribute I want to change, then change it, and then Save the file again? </p> <p>Any help/suggestions is greatly appreciated.</p> <p>Thank you</p> <p>Here's what my XML file looks like:</p> <pre><code> XElement xElement; xElement = new XElement("Orders"); XElement element = new XElement( "Order", new XAttribute("Id", CustomId), new XAttribute("Quantity", Quantity), new XAttribute("PartNo", PartNo), new XAttribute("Description", Description), new XAttribute("Discount", Discount), new XAttribute("Freight", Freight), new XAttribute("UnitValue", UnitValue), new XAttribute("LineTotal", LineTotal) ); xElement.Add(element); xElement.Save(PartNo + ".xml"); &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Orders&gt; &lt;Order Id="V45Y7B458B" Quantity="2" PartNo="5VNB98" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" /&gt; &lt;Order Id="jason" Quantity="2" PartNo="jason" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" /&gt; &lt;/Orders&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload