Home All Groups Group Topic Archive Search About
Author
12 Dec 2006 2:30 PM
Helen Trim
Please help!  I am wading through the documentation on XML and I can extract
a single element by it's tag using GetElementsByTagName.  But how do I
extract a nested tag.

For example:
<Order>
  <Name>
    <Code>JMS</Code>
    <CodeDescription>Dr J M Smith</CodeDescription>
  </Name>
  <Ward>
    <Code>P1</Code>
    <CodeDescription>Phillips</CodeDescription>
  </Ward>
</Order>

How do I get the Ward Code description and not the Name Code description ?

TIA

--
Helen

Author
12 Dec 2006 3:07 PM
_AnonCoward
Show quote Hide quote
"Helen Trim" <HelenT***@discussions.microsoft.com> wrote in message
news:F3747938-30E7-4C25-AA68-124010ABDF26@microsoft.com...
: Please help!  I am wading through the documentation on XML and I can
: extract a single element by it's tag using GetElementsByTagName.
: But how do I extract a nested tag.
:
: For example:
: <Order>
:  <Name>
:    <Code>JMS</Code>
:    <CodeDescription>Dr J M Smith</CodeDescription>
:  </Name>
:  <Ward>
:    <Code>P1</Code>
:    <CodeDescription>Phillips</CodeDescription>
:  </Ward>
: </Order>
:
: How do I get the Ward Code description and not the Name Code
: description ?
:
: TIA
:
: --
: Helen


If you know the structure of the xml in advance, then try:

    Dim x As New XmlDocument
    x.Load("Order.xml")
    Console.WriteLine(x.selectSingleNode("/Order/Ward/Code").innerText)

If the structure is variable, try some variation on this:

    x.selectSingleNode("//Ward/Code").innerText


Finally, this will work but as you see it's a lot of coding to acheive
the same result:

    Dim nodeList As XmlNodeList = x.getElementsByTagName("Code")
    For ndx As Integer = 0 To nodeList.count - 1
      If nodeList.item(ndx).parentNode.name = "Ward" Then
        Console.WriteLine(nodeList.item(ndx).innerText)
        Exit For
      End If
    Next

Ralf
--
--
----------------------------------------------------------
*             ^~^                   ^~^                  *
*          _ {~ ~}                 {~ ~} _               *
*         /_``>*<                   >*<''_\              *
*        (\--_)++)                 (++(_--/)             *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Author
12 Dec 2006 4:06 PM
Martin Honnen
Helen Trim wrote:
Show quoteHide quote
> Please help!  I am wading through the documentation on XML and I can extract
> a single element by it's tag using GetElementsByTagName.  But how do I
> extract a nested tag.
>
> For example:
> <Order>
>   <Name>
>     <Code>JMS</Code>
>     <CodeDescription>Dr J M Smith</CodeDescription>
>   </Name>
>   <Ward>
>     <Code>P1</Code>
>     <CodeDescription>Phillips</CodeDescription>
>   </Ward>
> </Order>
>
> How do I get the Ward Code description and not the Name Code description ?

Learning and using XPath makes that easy e.g. the XPath expression
   /Order/Ward/CodeDescription
selects the CodeDescription elements which have a Ward parent element
which have a root parent element Order. And then you can use
SelectSingleNode to access the first element e.g.
   Dim CodeDescription As XmlNode = _
xmlDocumentInstance.SelectSingleNode("/Order/Ward/CodeDescription")


--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
12 Dec 2006 4:51 PM
FishingScout
Like Martin stated, understanding XPath is the key to querying XML.  I
learned about XPath and XML transformations using a book titled XSLT &
XPATH a guide to XML transformations.

ISBN 0-13-040446-2

Its not bad once you understand it, but getting started can be
challenging.  I wrote a small application that loaded an XML document
and allowed me to type queries and execute the queries.  This helped me
test complex queries before having to test them in my code.  I also was
curious about the performance of queries so I used the applicaiton to
time how long the queries took. The performace is decent. For example,
I have an 3.5 MB XML file and I can query "//EVENT" ( all events ),
4000 in all in just 0.468 millisec.


Martin Honnen wrote:
Show quoteHide quote
> Helen Trim wrote:
> > Please help!  I am wading through the documentation on XML and I can extract
> > a single element by it's tag using GetElementsByTagName.  But how do I
> > extract a nested tag.
> >
> > For example:
> > <Order>
> >   <Name>
> >     <Code>JMS</Code>
> >     <CodeDescription>Dr J M Smith</CodeDescription>
> >   </Name>
> >   <Ward>
> >     <Code>P1</Code>
> >     <CodeDescription>Phillips</CodeDescription>
> >   </Ward>
> > </Order>
> >
> > How do I get the Ward Code description and not the Name Code description ?
>
> Learning and using XPath makes that easy e.g. the XPath expression
>    /Order/Ward/CodeDescription
> selects the CodeDescription elements which have a Ward parent element
> which have a root parent element Order. And then you can use
> SelectSingleNode to access the first element e.g.
>    Dim CodeDescription As XmlNode = _
> xmlDocumentInstance.SelectSingleNode("/Order/Ward/CodeDescription")
>
>
> --
>
>     Martin Honnen --- MVP XML
>     http://JavaScript.FAQTs.com/
Author
12 Dec 2006 5:04 PM
Cor Ligthert [MVP]
Helen,

I prefer the last years more the node reader

Have a look at the little sample at this page

http://www.vb-tips.com/dbpages.aspx?ID=e788c048-e547-4de3-9c6a-22589f018cd4

Cor


Show quoteHide quote
"Helen Trim" <HelenT***@discussions.microsoft.com> schreef in bericht
news:F3747938-30E7-4C25-AA68-124010ABDF26@microsoft.com...
> Please help!  I am wading through the documentation on XML and I can
> extract
> a single element by it's tag using GetElementsByTagName.  But how do I
> extract a nested tag.
>
> For example:
> <Order>
>  <Name>
>    <Code>JMS</Code>
>    <CodeDescription>Dr J M Smith</CodeDescription>
>  </Name>
>  <Ward>
>    <Code>P1</Code>
>    <CodeDescription>Phillips</CodeDescription>
>  </Ward>
> </Order>
>
> How do I get the Ward Code description and not the Name Code description ?
>
> TIA
>
> --
> Helen