473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Edit XML document

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
Jun 27 '08 #1
4 2011
gator wrote:
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
Jun 27 '08 #2
Arne Vajhøj wrote:
gator wrote:
>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.
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
Jun 27 '08 #3
On Jun 21, 10:46*pm, Arne Vajhøj <a...@vajhoej.dkwrote:
Arne Vajhøj wrote:
gator wrote:
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.

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
Jun 27 '08 #4
On Jun 22, 6:37*am, gator <glb...@gmail.comwrote:
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.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Claudio Jolowicz | last post by:
How can XSLT stylesheets be used to edit, remove and add nodes specified by their position in the document tree? The XML document stores development tasks in a hierarchical way, i.e. tasks can...
15
by: bengee | last post by:
Hi Does anyone know of a way to edit "live" javascript (that i have no control over) in IE? Example, i visit a website and see a webpage in IE containing Javascript. Can i edit that...
8
by: Gilles T. | last post by:
How I can get element ID in the edit mode of datagrid control? If I not in the edit mode, there are no problem. <asp:TemplateColumn ItemStyle-CssClass="grid_column_width_3"...
6
by: howardg | last post by:
I was wanting to use Javascript to detect the edit date on a series of .jpg files, and return that date in the corresponding HTML page, as in "Chart was last updated...". Does anyone know if this...
3
by: sygsix | last post by:
Hello all. I am a Java programmer who sometimes dabbles in simple PHP stuff, and had a question that's way over my head, for you DHTML experts. I would like to know how to dynamically edit a...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
2
by: torbjorn.maro | last post by:
I'm the developer of an enterprise level web application that among other things handles documents. The documents are stored in a database. There is now simple functionality to check out and...
2
by: sowmram | last post by:
Hi, I'm trying to create a site very similar to wiki, where you can edit, save.. Generally update data for the future use... This particular program I wanna do it using javascripts... Please...
6
by: Romulo NF | last post by:
Greetings again to everyone, Im back to show this grid componenet i´ve developed. With this grid you can show the data like a normal table, remove the rows that you need, add rows, import data,...
0
by: BHerz | last post by:
I have an Access 2007 application which displays the File Name of documents located on a Windows SharePoint V3 Site. When you click on the file name the file opens in "Read Only" format. I am not...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.