Please help newbie to parse xml

Please help newbie to parse xml

Post by Pau » Thu, 10 Oct 2002 01:20:14



Hi,

I have an xml file from which I wish to parse out certain items

The file has a schema at the top (omitted for brevity) - describing
the structure of the file?. (Please forgive my lack of knowledge but
I'm really just starting out) E.g. it begins

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <xsd:schema id="root" xmlns=""
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
.
.
.
  </xsd:schema>

Underneath the schema at (the same level of the <xsd:schema> node) are
a number of lines like the following.

  <resheader name="ResMimeType">
    <value>text/microsoft-resx</value>
  </resheader>

Underneath this again (again at the same level) I have what I wish to
parse out e.g.

  <data name="name1">
    <value>val1</value>
    <comment>comment1</comment>
  </data>
  <data name="name2">
    <value>val2</value>
    <comment>comment2</comment>
  </data>

All I wish to do is to loop through the entire document retrieving the
name of the 'data' node and the values of the associated 'value' and
'comment' nodes. For example

name1, val1 and comment1
name2, val2 and comment2.

I've read a bit about the different ways of parsing XML documents
(e.g. using DOM, XPath etc.) I'm not sure which to use. I don't need
to do any editing just retrieve the values so maybe the XPath will do.
I'd be grateful if somebody could provide an example of how to (1) go
to the first <data> node (2) how to loop through all <data> nodes
getting the values of the <value> and <comment> nodes using either the
DOM or XPath. If this is too much to ask a pointer to any good
examples would really be appreciated.

As an aside is it possible to validate the structure of an XML file
using a schema such as the one mentioned above.

Thanks

Paul

 
 
 

Please help newbie to parse xml

Post by Kirk Allen Evan » Thu, 10 Oct 2002 02:07:05


The simplest way to get started is to use an XmlNodeList and using the
SelectSingleNode method of the XmlNode class to get the values you are
looking for.

//Select all of the "data" elements
XmlNodeList nl = doc.SelectNodes("root/data");
foreach(XmlNode node in nl)
{
     //Output the node's name
     Console.Output(node.Name);

     Console.Output(node.SelectSingleNode("value/text()").Value);
     Console.Output(node.SelectSingleNode("comment/text()").Value);

Quote:}

There are better ways to do this, such as using the XPathNavigator and
XPathNodeIterator.  There are plenty of examples in the SDK docs on how to
use these two classes.  I could also recommend a great book   ; )

--
Kirk Allen Evans
http://www.xmlandasp.net
Author, "XML And ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X


Quote:> Hi,

> I have an xml file from which I wish to parse out certain items

> The file has a schema at the top (omitted for brevity) - describing
> the structure of the file?. (Please forgive my lack of knowledge but
> I'm really just starting out) E.g. it begins

> <?xml version="1.0" encoding="utf-8" ?>
> <root>
>   <xsd:schema id="root" xmlns=""
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
> .
> .
> .
>   </xsd:schema>

> Underneath the schema at (the same level of the <xsd:schema> node) are
> a number of lines like the following.

>   <resheader name="ResMimeType">
>     <value>text/microsoft-resx</value>
>   </resheader>

> Underneath this again (again at the same level) I have what I wish to
> parse out e.g.

>   <data name="name1">
>     <value>val1</value>
>     <comment>comment1</comment>
>   </data>
>   <data name="name2">
>     <value>val2</value>
>     <comment>comment2</comment>
>   </data>

> All I wish to do is to loop through the entire document retrieving the
> name of the 'data' node and the values of the associated 'value' and
> 'comment' nodes. For example

> name1, val1 and comment1
> name2, val2 and comment2.

> I've read a bit about the different ways of parsing XML documents
> (e.g. using DOM, XPath etc.) I'm not sure which to use. I don't need
> to do any editing just retrieve the values so maybe the XPath will do.
> I'd be grateful if somebody could provide an example of how to (1) go
> to the first <data> node (2) how to loop through all <data> nodes
> getting the values of the <value> and <comment> nodes using either the
> DOM or XPath. If this is too much to ask a pointer to any good
> examples would really be appreciated.

> As an aside is it possible to validate the structure of an XML file
> using a schema such as the one mentioned above.

> Thanks

> Paul


 
 
 

Please help newbie to parse xml

Post by Pau » Thu, 10 Oct 2002 19:51:37


Hi Kirk,

Many thanks for the help. Your example worked a treat!


> The simplest way to get started is to use an XmlNodeList and using the
> SelectSingleNode method of the XmlNode class to get the values you are
> looking for.

> //Select all of the "data" elements
> XmlNodeList nl = doc.SelectNodes("root/data");
> foreach(XmlNode node in nl)
> {
>      //Output the node's name
>      Console.Output(node.Name);

>      Console.Output(node.SelectSingleNode("value/text()").Value);
>      Console.Output(node.SelectSingleNode("comment/text()").Value);
> }

> There are better ways to do this, such as using the XPathNavigator and
> XPathNodeIterator.  There are plenty of examples in the SDK docs on how to
> use these two classes.  I could also recommend a great book   ; )

> --
> Kirk Allen Evans
> http://www.xmlandasp.net
> Author, "XML And ASP.NET", New Riders Publishing
> http://www.amazon.com/exec/obidos/ASIN/073571200X



> > Hi,

> > I have an xml file from which I wish to parse out certain items

> > The file has a schema at the top (omitted for brevity) - describing
> > the structure of the file?. (Please forgive my lack of knowledge but
> > I'm really just starting out) E.g. it begins

> > <?xml version="1.0" encoding="utf-8" ?>
> > <root>
> >   <xsd:schema id="root" xmlns=""
> > xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> > xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
> > .
> > .
> > .
> >   </xsd:schema>

> > Underneath the schema at (the same level of the <xsd:schema> node) are
> > a number of lines like the following.

> >   <resheader name="ResMimeType">
> >     <value>text/microsoft-resx</value>
> >   </resheader>

> > Underneath this again (again at the same level) I have what I wish to
> > parse out e.g.

> >   <data name="name1">
> >     <value>val1</value>
> >     <comment>comment1</comment>
> >   </data>
> >   <data name="name2">
> >     <value>val2</value>
> >     <comment>comment2</comment>
> >   </data>

> > All I wish to do is to loop through the entire document retrieving the
> > name of the 'data' node and the values of the associated 'value' and
> > 'comment' nodes. For example

> > name1, val1 and comment1
> > name2, val2 and comment2.

> > I've read a bit about the different ways of parsing XML documents
> > (e.g. using DOM, XPath etc.) I'm not sure which to use. I don't need
> > to do any editing just retrieve the values so maybe the XPath will do.
> > I'd be grateful if somebody could provide an example of how to (1) go
> > to the first <data> node (2) how to loop through all <data> nodes
> > getting the values of the <value> and <comment> nodes using either the
> > DOM or XPath. If this is too much to ask a pointer to any good
> > examples would really be appreciated.

> > As an aside is it possible to validate the structure of an XML file
> > using a schema such as the one mentioned above.

> > Thanks

> > Paul

 
 
 

1. vb.net newbie needs help with basic XML parsing?

I have an app that is a little strange.
I am connected via ethernet & tcp/ip to a device.

I make a request via a "telnet" connection to it and it returns the
following information as a string.  (vbcrlf) means it has a carriage rtn &
line feed in the string.

"1(vbcrlf)27(vbcrlf)<FLOAT>216.000015</FLOAT>(vbcrlf)"

two questions

Is there an easy way to get just what is between the FLOAT tags?

How do I handle it if there is many <FLOAT> tags in the string?

thanks for any help.

2. FS2002 - Program Too Big To Fit In Memory error

3. XMl Parsing Error - Please help

4. Web Developer with Java or C or C++, Arbor, MI,

5. Newbie... Parse String into A XML Schema/dataset

6. Sendmail 8.7.1 won't .forward for me!

7. cant load XML into DATASET. PLEASe PLEASE HELP!!!!!!

8. Indy IV: What is this?!

9. Help/advice on XML parsing values

10. Need help with XML parsing, XPath, nodes and other strange stuff *s*

11. How to parse "complex" XML - help

12. need help parsing xml

13. Xml Parsing Error sending valid Xml to stored procedure from ADO.Net / C#