all groups > dotnet xml > january 2006 >
You're in the

dotnet xml

group:

How to change attributes in an xml-file


How to change attributes in an xml-file Philipp
1/31/2006 3:20:32 PM
dotnet xml:
Hi,

I wrote a small c# programm, which generates an xml-string by serializing a
class and sends it to a server. Now I want to change some values in that
xml-string.
The only way for me to do that might be, to deserialize it, change the
particular attribute and serialize it again, to finally save it on my
harddisk.
But I believe, that there is a more convenient way to do this, since it is
also possible to append an element...

Can anybody help me in this matter? What about the class XmlAttribute?
I appreciate for you help.

Cheers
Philipp

Re: How to change attributes in an xml-file Martin Honnen
1/31/2006 3:30:55 PM


[quoted text, click to view]


[quoted text, click to view]

If you have a string with an XML document then you need to deserialize
to manipulate that. You can do that as you already seem to do, by
deserializing to an instance object of your class and manipulating the
properties of the object. Or you could use XmlDocument, the DOM
implementation in .NET to do that e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(yourString);
// now find the element whose attribute you want to change e.g.
XmlElement element =
xmlDocument.SelectSingleNode("SomeXPathExpression") as XmlElement;
if (element != null) {
element.SetAttribute("attribute-name", "new attribute value");
}
// how serialize to string e.g. using
xmlDocument.OuterXml


--

Martin Honnen --- MVP XML
Re: How to change attributes in an xml-file Philipp
1/31/2006 4:11:17 PM
OK, I´ve solved the problem. It was just to easy to see :-)

xdoc.SelectSingleNode("root/node1/node2").InnerText = "123456";

Bye,
Philipp



"Philipp" <fints@suedmeyer.net> schrieb im Newsbeitrag
news:449a1hFucuvU1@news.dfncis.de...
[quoted text, click to view]

AddThis Social Bookmark Button