How to update, remove and insert attributes in an XML file?

B

barbara_dave

Hi All,

I want to modify the specified attributes in an XML file. I know the
attributes' names, I just need to change the values of these
attributes.

I did some research and I think I need to use an XMLDocument object to
load the XML file, and use XPathNavigator to retrieve the nodes. But I
can't find a sample for that. Could some one please give me a sample
code for this?

Thanks a lot in advance!
 
J

Jon Skeet [C# MVP]

I want to modify the specified attributes in an XML file. I know the
attributes' names, I just need to change the values of these
attributes.

I did some research and I think I need to use an XMLDocument object to
load the XML file, and use XPathNavigator to retrieve the nodes. But I
can't find a sample for that. Could some one please give me a sample
code for this?

Well, which bit is causing you hassle? I personally wouldn't bother
with the XPathNavigator unless you need the performance - I'd just
load up the XML, use SelectSingleNode or SelectNodes to find the
relevant things to change, and then save again.

If you could give an example of the code you've got so far, that would
be helpful.

Jon
 
B

barbara_dave

Well, which bit is causing you hassle? I personally wouldn't bother
with the XPathNavigator unless you need the performance - I'd just
load up the XML, use SelectSingleNode or SelectNodes to find the
relevant things to change, and then save again.

If you could give an example of the code you've got so far, that would
be helpful.

Jon

The following is the XML:
<?xml version="1.0" encoding="utf-8"?>
<MyXml>
<Element1 attr1="a" attr2="b" attr3="c" attr4="d" />
<Element2 attr5="e" />
<Element3 attr6="f" attr7="h" attr8="i" />
<Element4 attr9="j" attr10="g" />
<Element5 attr11="k" attr12="l" />
<Element6 attr13="m" attr14="n" />
<Element4/>
<MyXml/>
The xml file path is xmlFile.

my code is:
private XmlDocument _Document;
_Document = new XmlDocument();
_Document.Load(xmlFile);

if I want to change the values of attr10 and attr12. How to access to
them?

Thanks!
 
J

Jon Skeet [C# MVP]

The following is the XML:
<?xml version="1.0" encoding="utf-8"?>
<MyXml>
<Element1 attr1="a" attr2="b" attr3="c" attr4="d" />
<Element2 attr5="e" />
<Element3 attr6="f" attr7="h" attr8="i" />
<Element4 attr9="j" attr10="g" />
<Element5 attr11="k" attr12="l" />
<Element6 attr13="m" attr14="n" />
<Element4/>
<MyXml/>
The xml file path is xmlFile.

my code is:
private XmlDocument _Document;
_Document = new XmlDocument();
_Document.Load(xmlFile);

if I want to change the values of attr10 and attr12. How to access to
them?

Use _Document.SelectSingleNode to get to the attribute, and then change
the value - or select the element and then change the attribute's value
from there.

You'll need to know XPath though - see http://w3schools.com/xpath
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I want to modify the specified attributes in an XML file. I know the
attributes' names, I just need to change the values of these
attributes.

I did some research and I think I need to use an XMLDocument object to
load the XML file, and use XPathNavigator to retrieve the nodes. But I
can't find a sample for that. Could some one please give me a sample
code for this?

Something like:

doc.DocumentElement.SelectSingleNode(xpathtoelement).Attributes[attrnam].Value
= newval;

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top