Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2003

Tip: Looking for answers? Try searching our database.

Changing Node Values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike McCarthy - 29 Jul 2003 17:33 GMT
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" ?
Oleg Tkachenko - 29 Jul 2003 18:45 GMT
> How can I iterate through using C# and change "Stri" to "ABC" ?

            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";

Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Mike McCarthy - 29 Jul 2003 19:18 GMT
When I run this I get an exception "Data at the root level is invalid".
Any suggestions?

> > How can I iterate through using C# and change "Stri" to "ABC" ?
>
>              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 - 29 Jul 2003 19:42 GMT
> When I run this I get an exception "Data at the root level is invalid".
> Any suggestions?
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.
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Mike McCarthy - 29 Jul 2003 19:56 GMT
Thanks the code runs.

I noticed though that my file has not been changed.

Should I be using XmlWriter ?

> > When I run this I get an exception "Data at the root level is invalid".
> > Any suggestions?
> 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 - 29 Jul 2003 20:17 GMT
> I noticed though that my file has not been changed.
Sure, you have to save modified document using Save method, e.g.
doc.Save("foo.xml");
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Mike McCarthy - 29 Jul 2003 20:27 GMT
Thank you very much.

> > I noticed though that my file has not been changed.
> Sure, you have to save modified document using Save method, e.g.
> doc.Save("foo.xml");

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.