I want to change the value of an existing attribute's value using C#.
I can't seem to find the answer to this on the Googles, probably because I don't know the terminology well enough to ask the question intelligently.
I can find:
- lots of ways to write a new xml doc, with new elements\attributes
- ways to read the value of an existing element's attribute
But I can't find a way to drill down to an already existing element's attribute and change the value of that attribute.
To provide context, let's say that this is the xml file called "stuff.xml" located at C:\stuff.xml:
<?xml version="1.0" encoding="utf-8"
<configuration>
<add key="apple"
value="red"/>
<add key="school_bus"
value="yellow"/>
<add key="grass"
value="orange"/>
<system.Net>
<binding>
<endpoint address="https://1111.11.11.11:7276/Service"
binding="basicHttpBinding"/>
<endpoint address="http://localhost/Service"
binding="advancedHttpBinding"/>
</binding>
</configuration>
What I'd like to do -- without using LINQ -- is to change the color value for grass to "green" and change the address for the first endpoint to https://222.22.22.22:7276/Service
.
(I'm not married to not using LINQ, just, as a non-LINQ user I find it to be unreadable.)
I'm sure there's a way to do this! (And it's probably easy to find -- I just can't find it.)
I imagine that the answer will use the XmlWriter object, it's just that I don't know how to drill down to a sub-element and then use the XmlWriter object.
@maccettura: your suspicious mind is correct... sort of. It is a config file, but it's not the app.config file. And it has to change. Um... serialize. OK. I've sort of looked at that, but I always thought that to serialize was to basically read the values of the entire file into an object, then you could do with it what you will... I will look into that. Thank you.