469,963 Members | 2,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

interacting with xml


hi guys , I'm mew to system.xml my question is :
what is the right way to read / write value to the "First_Name" node ?
and same question : how do I read/ write to the category property (inside
the CD node) ?

thank you very much
Sharon

<Catalog>
<Cd category="rock">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category="pop">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>
Nov 17 '05 #1
7 1141
Hi there... you can do it like this...

System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); // Used to load
the xml document
System.Xml.XmlNode node1, node2; // Used to store information about those
nodes you're interested in
string node1_value, node2_value; // Used to store and read the values from
the nodes
doc.Load(@"c:\sample.xml"); // Loading of xml document
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist"); //
Select the node by using a XPath query
node1_value = node1["First_Name"].InnerText; // Store the value of the first
node
node1["First_Name"].InnerXml = "Angel"; // Change the value... I wrote my
name ;-)
node2 = doc.SelectSingleNode("Catalog/Cd[@category='pop']"); // Select the
node once again by using XPath
node2_value = node2.Attributes["category"].InnerText; // Get the value for
the attribute
node2.Attributes["category"].InnerXml="jazz"; // Set the new value for the
attribute
doc.Save(@"c:\modified.xml"); // Save the modified document

and that's it! The catalog's closing tag is missing in the xml you send so
you need to write it first in order to test the code.
Hope this may help you... Have a nice day! :D

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com
"Sharon" <Sh*******@hotmail.com> escribió en el mensaje
news:Om**************@TK2MSFTNGP15.phx.gbl...

hi guys , I'm mew to system.xml my question is :
what is the right way to read / write value to the "First_Name" node ?
and same question : how do I read/ write to the category property (inside
the CD node) ?

thank you very much
Sharon

<Catalog>
<Cd category="rock">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category="pop">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>

Nov 17 '05 #2
Thank you Angel

After changing from this line:
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
to this :

node1 = doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

It’s working great!!!

Thank you very much

Sharon

Angel J. Hernández M." <an**********@hotmail.com> wrote in message
news:eH**************@TK2MSFTNGP15.phx.gbl...
Hi there... you can do it like this...

System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); // Used to load
the xml document
System.Xml.XmlNode node1, node2; // Used to store information about those
nodes you're interested in
string node1_value, node2_value; // Used to store and read the values from
the nodes
doc.Load(@"c:\sample.xml"); // Loading of xml document
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist"); //
Select the node by using a XPath query
node1_value = node1["First_Name"].InnerText; // Store the value of the
first node
node1["First_Name"].InnerXml = "Angel"; // Change the value... I wrote my
name ;-)
node2 = doc.SelectSingleNode("Catalog/Cd[@category='pop']"); // Select the
node once again by using XPath
node2_value = node2.Attributes["category"].InnerText; // Get the value for
the attribute
node2.Attributes["category"].InnerXml="jazz"; // Set the new value for the
attribute
doc.Save(@"c:\modified.xml"); // Save the modified document

and that's it! The catalog's closing tag is missing in the xml you send so
you need to write it first in order to test the code.
Hope this may help you... Have a nice day! :D

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com
"Sharon" <Sh*******@hotmail.com> escribió en el mensaje
news:Om**************@TK2MSFTNGP15.phx.gbl...

hi guys , I'm mew to system.xml my question is :
what is the right way to read / write value to the "First_Name" node ?
and same question : how do I read/ write to the category property (inside
the CD node) ?

thank you very much
Sharon

<Catalog>
<Cd category="rock">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category="pop">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>


Nov 17 '05 #3
You're welcome!!! :D Happy Programming

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com
"Sharon" <Sh*******@hotmail.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thank you Angel

After changing from this line:
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
to this :

node1 = doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

It's working great!!!

Thank you very much

Sharon

Angel J. Hernández M." <an**********@hotmail.com> wrote in message
news:eH**************@TK2MSFTNGP15.phx.gbl...
Hi there... you can do it like this...

System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); // Used to
load the xml document
System.Xml.XmlNode node1, node2; // Used to store information about those
nodes you're interested in
string node1_value, node2_value; // Used to store and read the values
from the nodes
doc.Load(@"c:\sample.xml"); // Loading of xml document
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist"); //
Select the node by using a XPath query
node1_value = node1["First_Name"].InnerText; // Store the value of the
first node
node1["First_Name"].InnerXml = "Angel"; // Change the value... I wrote my
name ;-)
node2 = doc.SelectSingleNode("Catalog/Cd[@category='pop']"); // Select
the node once again by using XPath
node2_value = node2.Attributes["category"].InnerText; // Get the value
for the attribute
node2.Attributes["category"].InnerXml="jazz"; // Set the new value for
the attribute
doc.Save(@"c:\modified.xml"); // Save the modified document

and that's it! The catalog's closing tag is missing in the xml you send
so you need to write it first in order to test the code.
Hope this may help you... Have a nice day! :D

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com
"Sharon" <Sh*******@hotmail.com> escribió en el mensaje
news:Om**************@TK2MSFTNGP15.phx.gbl...

hi guys , I'm mew to system.xml my question is :
what is the right way to read / write value to the "First_Name" node ?
and same question : how do I read/ write to the category property
(inside the CD node) ?

thank you very much
Sharon

<Catalog>
<Cd category="rock">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category="pop">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>



Nov 17 '05 #4
Sharon <Sh*******@hotmail.com> wrote:
Thank you Angel

After changing from this line:
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
to this :

node1 = doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

It=3Fs working great!!!


Hmm... I don't think you should have needed to do that (and if you
don't have to, then obviously the first is more readable :)

Could you post a short but complete program which shows the difference
between them.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example showing them giving the same answer:

using System;
using System.Xml;

class Test
{
static void Main()
{
string xml = @"<?xml version=""1.0"" ?>
<Catalog>
<Cd category=""rock"">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category=""pop"">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>
</Catalog>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode node1 = doc.SelectSingleNode
("Catalog/Cd[@category=\"rock\"]/Artist");
XmlNode node2 = doc.SelectSingleNode
("Catalog/Cd[@category='rock']/Artist");

Console.WriteLine (node1==node2);
}
}

It prints out "True" showing that the results are the same for both
calls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
My env is

Xp pro

Net 2003 VER 7.1.3088

Microsoft .NET Framework SDK v1.1

But I tested the code today and it's working fine (both versions :)

v1 : node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
and this :
v2 : node1 = doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

I don't know how is it possible but v1 returned null yesterday
:( and I swear didn't touch the code

Sorry

Sharon

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sharon <Sh*******@hotmail.com> wrote:
Thank you Angel

After changing from this line:
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
to this :

node1 =
doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

It=3Fs working great!!!


Hmm... I don't think you should have needed to do that (and if you
don't have to, then obviously the first is more readable :)

Could you post a short but complete program which shows the difference
between them.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example showing them giving the same answer:

using System;
using System.Xml;

class Test
{
static void Main()
{
string xml = @"<?xml version=""1.0"" ?>
<Catalog>
<Cd category=""rock"">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category=""pop"">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>
</Catalog>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode node1 = doc.SelectSingleNode
("Catalog/Cd[@category=\"rock\"]/Artist");
XmlNode node2 = doc.SelectSingleNode
("Catalog/Cd[@category='rock']/Artist");

Console.WriteLine (node1==node2);
}
}

It prints out "True" showing that the results are the same for both
calls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
You can also learn something from Dan Whalin's videos [1]

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://xmlforasp.net/

"Sharon" <Sh*******@hotmail.com> wrote in message
news:OS**************@TK2MSFTNGP15.phx.gbl...
My env is

Xp pro

Net 2003 VER 7.1.3088

Microsoft .NET Framework SDK v1.1

But I tested the code today and it's working fine (both versions :)

v1 : node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
and this :
v2 : node1 =
doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

I don't know how is it possible but v1 returned null yesterday
:( and I swear didn't touch the code

Sorry

Sharon

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sharon <Sh*******@hotmail.com> wrote:
Thank you Angel

After changing from this line:
node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
to this :

node1 =
doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");

It=3Fs working great!!!


Hmm... I don't think you should have needed to do that (and if you
don't have to, then obviously the first is more readable :)

Could you post a short but complete program which shows the difference
between them.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example showing them giving the same answer:

using System;
using System.Xml;

class Test
{
static void Main()
{
string xml = @"<?xml version=""1.0"" ?>
<Catalog>
<Cd category=""rock"">
<Title>Empire Burlesque</Title>
<Artist>
<First_Name>Bob</First_Name>
<Last_Name>Dylan</Last_Name>
</Artist>
<Country>Usa</Country>
<Company>Columbia</Company>
<Price>10.90</Price>
<Year>1985</Year>
</Cd>
<Cd category=""pop"">
<Title>Hide Your Heart</Title>
<Artist>
<First_Name>Bonnie</First_Name>
<Last_Name>Tyler</Last_Name>
</Artist>
<Country>Uk</Country>
<Company>Cbs Records</Company>
<Price>9.90</Price>
<Year>1988</Year>
</Cd>
</Catalog>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode node1 = doc.SelectSingleNode
("Catalog/Cd[@category=\"rock\"]/Artist");
XmlNode node2 = doc.SelectSingleNode
("Catalog/Cd[@category='rock']/Artist");

Console.WriteLine (node1==node2);
}
}

It prints out "True" showing that the results are the same for both
calls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #7
Sharon <Sh*******@hotmail.com> wrote:
But I tested the code today and it's working fine (both versions :)
v1 : node1 = doc.SelectSingleNode("Catalog/Cd[@category='rock']/Artist");
and this :
v2 : node1 = doc.SelectSingleNode("Catalog/Cd[@category=\"rock\"]/Artist");
I don't know how is it possible but v1 returned null yesterday
:( and I swear didn't touch the code


I suspect you touched *something* - it's just a case of working out
what :)

As I say, I'd go with the first version if I were you - it's a bit
easier to read!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andrew Crowe | last post: by
1 post views Thread by Hugh McLaughlin | last post: by
2 posts views Thread by John | last post: by
2 posts views Thread by Michael Quan | last post: by
reply views Thread by Paul Wagstaff | last post: by
1 post views Thread by Raman | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.