Edit XML document

G

gator

Hello,

I am trying to edit a specific node in an XML file using C#. I have
searched around for example code to do this. The only solutions I
have come across either add a new node to the doc or have me re-write
the entire doccument ... is there a way to open the doc, edit only a
specific node and then save the changes?

Here is an example of an XML document:

<?xml version="1.0"?>
<inventory>
<book>
<title>The Godfather</title>
<author>Mario Puzo</author>
<publisher>Signet</publisher>
<genre>Crime Drama</genre>
<price>$29.95</price>
<stock>5</stock>
</book>
</inventory>

How would I change only the "stock" node to "4" and save the file?

Thanks for your help,
Steve
 
A

Arne Vajhøj

gator said:
I am trying to edit a specific node in an XML file using C#. I have
searched around for example code to do this. The only solutions I
have come across either add a new node to the doc or have me re-write
the entire doccument ... is there a way to open the doc, edit only a
specific node and then save the changes?

Here is an example of an XML document:

<?xml version="1.0"?>
<inventory>
<book>
<title>The Godfather</title>
<author>Mario Puzo</author>
<publisher>Signet</publisher>
<genre>Crime Drama</genre>
<price>$29.95</price>
<stock>5</stock>
</book>
</inventory>

How would I change only the "stock" node to "4" and save the file?

Read the XML file into an XmlDocument, update the node and write it back
to a file.

Arne
 
A

Arne Vajhøj

Arne said:
Read the XML file into an XmlDocument, update the node and write it back
to a file.

Simple code:

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\i.xml");
doc.SelectSingleNode("//inventory/book/stock/text()").Value
= "4";
doc.Save(@"C:\o.xml");

Arne
 
G

gator

Simple code:

             XmlDocument doc = new XmlDocument();
             doc.Load(@"C:\i.xml");
             doc.SelectSingleNode("//inventory/book/stock/text()").Value
= "4";
             doc.Save(@"C:\o.xml");

Arne- Hide quoted text -

- Show quoted text -

Arne,

Yes! That's exactly what I needed. Thanks for the help AND the very
quick response!!

Steve
 
P

Pavel Minaev

Hello,

I am trying to edit a specific node in an XML file using C#.  I have
searched around for example code to do this.  The only solutions I
have come across either add a new node to the doc or have me re-write
the entire doccument ... is there a way to open the doc, edit only a
specific node and then save the changes?

Here is an example of an XML document:

<?xml version="1.0"?>
<inventory>
    <book>
        <title>The Godfather</title>
        <author>Mario Puzo</author>
        <publisher>Signet</publisher>
        <genre>Crime Drama</genre>
        <price>$29.95</price>
        <stock>5</stock>
    </book>
</inventory>

How would I change only the "stock" node to "4" and save the file?

Note that the proposed solution still recreates the entire file when
Save() is called - if you have originally mentioned it because of some
performance requirements, then it might not be the one to seek.

Also, it loads the entire XML file into memory. Again, it most likely
doesn't matter, but for multimegabyte documents, using XmlReader/
XmlWriter combo, passing through all elements unchanged, and only
modifying the values you need, will be much more memory-efficient.
 

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