all groups > dotnet xml > june 2007 >
You're in the

dotnet xml

group:

How to modify a attribute's value of a specific node in an xml fil



How to modify a attribute's value of a specific node in an xml fil Rakesh
6/28/2007 10:20:00 PM
dotnet xml: Hi,

I have to modify the xml document which is present in the local disc when
user changes the values in the form. I have to modify the attribute value of
a specific node and then save the xml file. Please let me know how this can
be done.

Thanks:
Re: How to modify a attribute's value of a specific node in an xml fil Bjoern Hoehrmann
6/29/2007 12:00:00 AM
* Rakesh wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

There are a number of ways to do this, you could use data binding, or a
data set, you can write a SAX-like filter, use .NET's pull parser, and
so on. If the XML document is small you could load it into a XmlDocument
locate and change the attribute and save it.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
Re: How to modify a attribute's value of a specific node in an xml Martin Honnen
6/29/2007 12:00:00 AM
[quoted text, click to view]

Select the element node using SelectNodes or SelectSingleNode and simply
call the SetAttribute method e.g.
XmlDocument doc = new XmlDocument();
doc.Load(@"file.xml");
XmlElement someElement = doc.SelectSingleNode(@"root/element") as
XmlElement;
if (someElement != null) {
someElement.SetAttribute("attribute-name", "new value");
doc.Save(@"file.xml");
}
--

Martin Honnen --- MVP XML
Re: How to modify a attribute's value of a specific node in an xml Rakesh
6/29/2007 12:34:02 AM
Thanks a bunch Bjoern for the response,

Could you please let me know how can I change the attribute value once I
load the xml file into the XML Document.

[quoted text, click to view]
Re: How to modify a attribute's value of a specific node in an xml Rakesh
6/29/2007 7:50:03 AM
Thanks a lot Martin,

It worked !!!



[quoted text, click to view]
Re: How to modify a attribute's value of a specific node in an xml Rakesh
6/29/2007 11:02:07 AM
Hi,

I am seeing other issue with your code. Below are the details:
The save statement is saving only that node changes and clearing all the
existing values. This blocks me in a trouble what would be the solution for
this...

[quoted text, click to view]
Re: How to modify a attribute's value of a specific node in an xml Martin Honnen
6/30/2007 1:59:43 PM
[quoted text, click to view]

I don't understand what the problem is. The Save method of an
XmlDocument saves the complete document.


--

Martin Honnen --- MVP XML
Re: How to modify a attribute's value of a specific node in an xml Rakesh
7/3/2007 12:10:02 AM
Sorry. My bad It was due to my mistake. Code is working just fine. Thanks a
lot...

[quoted text, click to view]
AddThis Social Bookmark Button