all groups > dotnet xml > july 2003 >
You're in the

dotnet xml

group:

Changing Node Values



Changing Node Values Mike McCarthy
7/29/2003 12:33:21 PM
dotnet xml:

I have the following XML..

<?xml version="1.0" encoding="UTF-8"?>

<EMS>

<Scene>

<Number_of_Patients>1212</Number_of_Patients>

<MCI code_description_type="description">

<Code>Stri</Code>

</MCI>

<Location_Type code_description_type="description">

<Code>Stri</Code>

</Location_Type>

<Service_Type code_description_type="description">

<Code>Stri</Code>

</Service_Type>

</Scene>

</EMS>


using the following Xpath expression ...
//*[@code_description_type='description']//Code

How can I iterate through using C# and change "Stri" to "ABC" ?

Re: Changing Node Values Mike McCarthy
7/29/2003 2:18:20 PM
When I run this I get an exception "Data at the root level is invalid".
Any suggestions?


[quoted text, click to view]
doc.SelectNodes("//*[@code_description_type='description']//Code[.='Stri']")
;
[quoted text, click to view]

Re: Changing Node Values Mike McCarthy
7/29/2003 2:56:07 PM
Thanks the code runs.

I noticed though that my file has not been changed.

Should I be using XmlWriter ?

[quoted text, click to view]

Re: Changing Node Values Mike McCarthy
7/29/2003 3:27:44 PM
Thank you very much.

[quoted text, click to view]

Re: Changing Node Values Oleg Tkachenko
7/29/2003 7:45:14 PM
[quoted text, click to view]

XmlDocument doc = new XmlDocument();
//Load XML
doc.LoadXml(xml);
XmlNodeList list =
doc.SelectNodes("//*[@code_description_type='description']//Code[.='Stri']");
foreach (XmlNode node in list)
node.FirstChild.Value = "ABC";

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Re: Changing Node Values Oleg Tkachenko
7/29/2003 9:42:55 PM
[quoted text, click to view]
Most likely you are trying to load XML by URL using LoadXML method. My
mistake, I just tested it loading your XML as string. Use ordinar Load
method instead.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Re: Changing Node Values Oleg Tkachenko
7/29/2003 10:17:04 PM
[quoted text, click to view]
Sure, you have to save modified document using Save method, e.g.
doc.Save("foo.xml");
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
AddThis Social Bookmark Button