Home All Groups Group Topic Archive Search About

another vb .net xml question

Author
21 Nov 2006 3:45 PM
mattdaddym
Hi all,

I've taken a couple of hours to read what is available, and I still
cannot figure out how to do a very simple task in vb .net...lol.

All I need to do is read an xml file and parse out specific information
based on simple criteria. Let's use this as the xml file:

<?xml version="1.0" encoding="utf-8" ?>
<sites>
  <siteData>
     <siteName>flux</siteName>
     <active>true</active>
     <email>m*@me.com</email>
     <email>y**@you.com</email>
  </siteData>
  <siteData>
     <siteName>capacitor</siteName>
     <active>false</active>
     <email>y*@yo.com</email>
  </siteData>
  <siteData>
    <siteName>scott</siteName>
    <active>true</active>
    <email>y**@you.com</email>
    <email>m*@me.com</email>
    <email>dup***@dupree.com</email>
  </siteData>
</sites>

I need to read out the email addresses as strings based on the
siteName.

I have seen at least 3 distinct ways to read xml.

1) Throw it into a dataset with DATASET.READXML

2) Use a xmldatareader  XMLREADER.CREATE(BLAH,BLAH)

3) Instantiate on xmldocument and load the file in  myXmlDoc as new
XmlDocument / myXmlDoc.Load(blah)

I can load the file and display it, but I am having trouble with the
syntax for reading out the value/innertext of one of the nodes based on
another node value. So here is what exactly I would like to do.

Traverse the xml file to the <siteData> node that contains whatever
<siteName> I specify. Then I need to iterate through the <email> nodes
(each site may have from 1 to 3 so I won't know how many)

Any help is greatly appreciated. Thanks!

Author
21 Nov 2006 4:52 PM
rowe_newsgroups
Well, the below is really crude and there's probably a better way, but
here's what I could think of without trying to hard. Just put it in a
console project and you should be set (by the way, watch for word wrap)

Thanks,

Seth Rowe


    Sub Main()

        Dim DDIR As String = "C:\Documents and
Settings\srowe\Desktop\test.xml" ' your path to the xml file here
        Dim doc As New Xml.XmlDocument
        doc.Load(DDIR)
        Dim nlist As Xml.XmlNodeList =
doc.GetElementsByTagName("siteData")
        For Each n As Xml.XmlNode In nlist
            For i As Integer = 0 To n.ChildNodes.Count - 1
                Dim cn As Xml.XmlNode = n.ChildNodes(i)
                If cn.Name = "siteName" Then
                    If cn.InnerText = "scott" Then ' Site Name to
search for
                        Console.WriteLine(cn.InnerText)
                        ' start another loop to make sure we get the
emails if siteName
                        ' is not the first node in the child nodes
                        For Each cn2 As Xml.XmlNode In n.ChildNodes
                            If cn2.Name = "email" Then
                                Console.WriteLine(ControlChars.Tab &
cn2.InnerText)
                            End If
                        Next
                        Console.Read()
                        Exit Sub
                    Else
                        Exit For
                    End If
                Else
                    Continue For
                End If
            Next
        Next
        Console.Read()

    End Sub


mattdad***@gmail.com wrote:
Show quoteHide quote
> Hi all,
>
> I've taken a couple of hours to read what is available, and I still
> cannot figure out how to do a very simple task in vb .net...lol.
>
> All I need to do is read an xml file and parse out specific information
> based on simple criteria. Let's use this as the xml file:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <sites>
>   <siteData>
>      <siteName>flux</siteName>
>      <active>true</active>
>      <email>m*@me.com</email>
>      <email>y**@you.com</email>
>   </siteData>
>   <siteData>
>      <siteName>capacitor</siteName>
>      <active>false</active>
>      <email>y*@yo.com</email>
>   </siteData>
>   <siteData>
>     <siteName>scott</siteName>
>     <active>true</active>
>     <email>y**@you.com</email>
>     <email>m*@me.com</email>
>     <email>dup***@dupree.com</email>
>   </siteData>
> </sites>
>
> I need to read out the email addresses as strings based on the
> siteName.
>
> I have seen at least 3 distinct ways to read xml.
>
> 1) Throw it into a dataset with DATASET.READXML
>
> 2) Use a xmldatareader  XMLREADER.CREATE(BLAH,BLAH)
>
> 3) Instantiate on xmldocument and load the file in  myXmlDoc as new
> XmlDocument / myXmlDoc.Load(blah)
>
> I can load the file and display it, but I am having trouble with the
> syntax for reading out the value/innertext of one of the nodes based on
> another node value. So here is what exactly I would like to do.
>
> Traverse the xml file to the <siteData> node that contains whatever
> <siteName> I specify. Then I need to iterate through the <email> nodes
> (each site may have from 1 to 3 so I won't know how many)
>
> Any help is greatly appreciated. Thanks!
Author
21 Nov 2006 7:14 PM
mattdaddym
Yeah, it seems like there would be a more elegant way to iterate
through the list, BUT I AM VERY THANKFUL for what you posted. :) It has
me back on the right track. Thank you!
rowe_newsgroups wrote:
Show quoteHide quote
> Well, the below is really crude and there's probably a better way, but
> here's what I could think of without trying to hard. Just put it in a
> console project and you should be set (by the way, watch for word wrap)
>
> Thanks,
>
> Seth Rowe
>
>
>     Sub Main()
>
>         Dim DDIR As String = "C:\Documents and
> Settings\srowe\Desktop\test.xml" ' your path to the xml file here
>         Dim doc As New Xml.XmlDocument
>         doc.Load(DDIR)
>         Dim nlist As Xml.XmlNodeList =
> doc.GetElementsByTagName("siteData")
>         For Each n As Xml.XmlNode In nlist
>             For i As Integer = 0 To n.ChildNodes.Count - 1
>                 Dim cn As Xml.XmlNode = n.ChildNodes(i)
>                 If cn.Name = "siteName" Then
>                     If cn.InnerText = "scott" Then ' Site Name to
> search for
>                         Console.WriteLine(cn.InnerText)
>                         ' start another loop to make sure we get the
> emails if siteName
>                         ' is not the first node in the child nodes
>                         For Each cn2 As Xml.XmlNode In n.ChildNodes
>                             If cn2.Name = "email" Then
>                                 Console.WriteLine(ControlChars.Tab &
> cn2.InnerText)
>                             End If
>                         Next
>                         Console.Read()
>                         Exit Sub
>                     Else
>                         Exit For
>                     End If
>                 Else
>                     Continue For
>                 End If
>             Next
>         Next
>         Console.Read()
>
>     End Sub
>
>
> mattdad***@gmail.com wrote:
> > Hi all,
> >
> > I've taken a couple of hours to read what is available, and I still
> > cannot figure out how to do a very simple task in vb .net...lol.
> >
> > All I need to do is read an xml file and parse out specific information
> > based on simple criteria. Let's use this as the xml file:
> >
> > <?xml version="1.0" encoding="utf-8" ?>
> > <sites>
> >   <siteData>
> >      <siteName>flux</siteName>
> >      <active>true</active>
> >      <email>m*@me.com</email>
> >      <email>y**@you.com</email>
> >   </siteData>
> >   <siteData>
> >      <siteName>capacitor</siteName>
> >      <active>false</active>
> >      <email>y*@yo.com</email>
> >   </siteData>
> >   <siteData>
> >     <siteName>scott</siteName>
> >     <active>true</active>
> >     <email>y**@you.com</email>
> >     <email>m*@me.com</email>
> >     <email>dup***@dupree.com</email>
> >   </siteData>
> > </sites>
> >
> > I need to read out the email addresses as strings based on the
> > siteName.
> >
> > I have seen at least 3 distinct ways to read xml.
> >
> > 1) Throw it into a dataset with DATASET.READXML
> >
> > 2) Use a xmldatareader  XMLREADER.CREATE(BLAH,BLAH)
> >
> > 3) Instantiate on xmldocument and load the file in  myXmlDoc as new
> > XmlDocument / myXmlDoc.Load(blah)
> >
> > I can load the file and display it, but I am having trouble with the
> > syntax for reading out the value/innertext of one of the nodes based on
> > another node value. So here is what exactly I would like to do.
> >
> > Traverse the xml file to the <siteData> node that contains whatever
> > <siteName> I specify. Then I need to iterate through the <email> nodes
> > (each site may have from 1 to 3 so I won't know how many)
> >
> > Any help is greatly appreciated. Thanks!
Author
21 Nov 2006 7:55 PM
rowe_newsgroups
Are you stuck with the way that XML file is laid out or could you
reformat it? If, for example, the <email> nodes where children of the
<siteName> nodes I could write a much better algorithm.

Thanks,

Seth Rowe


mattdad***@gmail.com wrote:
Show quoteHide quote
> Yeah, it seems like there would be a more elegant way to iterate
> through the list, BUT I AM VERY THANKFUL for what you posted. :) It has
> me back on the right track. Thank you!
> rowe_newsgroups wrote:
> > Well, the below is really crude and there's probably a better way, but
> > here's what I could think of without trying to hard. Just put it in a
> > console project and you should be set (by the way, watch for word wrap)
> >
> > Thanks,
> >
> > Seth Rowe
> >
> >
> >     Sub Main()
> >
> >         Dim DDIR As String = "C:\Documents and
> > Settings\srowe\Desktop\test.xml" ' your path to the xml file here
> >         Dim doc As New Xml.XmlDocument
> >         doc.Load(DDIR)
> >         Dim nlist As Xml.XmlNodeList =
> > doc.GetElementsByTagName("siteData")
> >         For Each n As Xml.XmlNode In nlist
> >             For i As Integer = 0 To n.ChildNodes.Count - 1
> >                 Dim cn As Xml.XmlNode = n.ChildNodes(i)
> >                 If cn.Name = "siteName" Then
> >                     If cn.InnerText = "scott" Then ' Site Name to
> > search for
> >                         Console.WriteLine(cn.InnerText)
> >                         ' start another loop to make sure we get the
> > emails if siteName
> >                         ' is not the first node in the child nodes
> >                         For Each cn2 As Xml.XmlNode In n.ChildNodes
> >                             If cn2.Name = "email" Then
> >                                 Console.WriteLine(ControlChars.Tab &
> > cn2.InnerText)
> >                             End If
> >                         Next
> >                         Console.Read()
> >                         Exit Sub
> >                     Else
> >                         Exit For
> >                     End If
> >                 Else
> >                     Continue For
> >                 End If
> >             Next
> >         Next
> >         Console.Read()
> >
> >     End Sub
> >
> >
> > mattdad***@gmail.com wrote:
> > > Hi all,
> > >
> > > I've taken a couple of hours to read what is available, and I still
> > > cannot figure out how to do a very simple task in vb .net...lol.
> > >
> > > All I need to do is read an xml file and parse out specific information
> > > based on simple criteria. Let's use this as the xml file:
> > >
> > > <?xml version="1.0" encoding="utf-8" ?>
> > > <sites>
> > >   <siteData>
> > >      <siteName>flux</siteName>
> > >      <active>true</active>
> > >      <email>m*@me.com</email>
> > >      <email>y**@you.com</email>
> > >   </siteData>
> > >   <siteData>
> > >      <siteName>capacitor</siteName>
> > >      <active>false</active>
> > >      <email>y*@yo.com</email>
> > >   </siteData>
> > >   <siteData>
> > >     <siteName>scott</siteName>
> > >     <active>true</active>
> > >     <email>y**@you.com</email>
> > >     <email>m*@me.com</email>
> > >     <email>dup***@dupree.com</email>
> > >   </siteData>
> > > </sites>
> > >
> > > I need to read out the email addresses as strings based on the
> > > siteName.
> > >
> > > I have seen at least 3 distinct ways to read xml.
> > >
> > > 1) Throw it into a dataset with DATASET.READXML
> > >
> > > 2) Use a xmldatareader  XMLREADER.CREATE(BLAH,BLAH)
> > >
> > > 3) Instantiate on xmldocument and load the file in  myXmlDoc as new
> > > XmlDocument / myXmlDoc.Load(blah)
> > >
> > > I can load the file and display it, but I am having trouble with the
> > > syntax for reading out the value/innertext of one of the nodes based on
> > > another node value. So here is what exactly I would like to do.
> > >
> > > Traverse the xml file to the <siteData> node that contains whatever
> > > <siteName> I specify. Then I need to iterate through the <email> nodes
> > > (each site may have from 1 to 3 so I won't know how many)
> > >
> > > Any help is greatly appreciated. Thanks!
Author
21 Nov 2006 10:25 PM
Chris Dunaway
rowe_newsgroups wrote:
Show quoteHide quote
> Are you stuck with the way that XML file is laid out or could you
> reformat it? If, for example, the <email> nodes where children of the
> <siteName> nodes I could write a much better algorithm.
>
> Thanks,
>
> Seth Rowe
>
>
> mattdad***@gmail.com wrote:
> > Yeah, it seems like there would be a more elegant way to iterate
> > through the list, BUT I AM VERY THANKFUL for what you posted. :) It has
> > me back on the right track. Thank you!
> > rowe_newsgroups wrote:
> > > Well, the below is really crude and there's probably a better way, but
> > > here's what I could think of without trying to hard. Just put it in a
> > > console project and you should be set (by the way, watch for word wrap)
> > >
> > > Thanks,
> > >
> > > Seth Rowe
> > >
> > >
> > >     Sub Main()
> > >
> > >         Dim DDIR As String = "C:\Documents and
> > > Settings\srowe\Desktop\test.xml" ' your path to the xml file here
> > >         Dim doc As New Xml.XmlDocument
> > >         doc.Load(DDIR)
> > >         Dim nlist As Xml.XmlNodeList =
> > > doc.GetElementsByTagName("siteData")
> > >         For Each n As Xml.XmlNode In nlist
> > >             For i As Integer = 0 To n.ChildNodes.Count - 1
> > >                 Dim cn As Xml.XmlNode = n.ChildNodes(i)
> > >                 If cn.Name = "siteName" Then
> > >                     If cn.InnerText = "scott" Then ' Site Name to
> > > search for
> > >                         Console.WriteLine(cn.InnerText)
> > >                         ' start another loop to make sure we get the
> > > emails if siteName
> > >                         ' is not the first node in the child nodes
> > >                         For Each cn2 As Xml.XmlNode In n.ChildNodes
> > >                             If cn2.Name = "email" Then
> > >                                 Console.WriteLine(ControlChars.Tab &
> > > cn2.InnerText)
> > >                             End If
> > >                         Next
> > >                         Console.Read()
> > >                         Exit Sub
> > >                     Else
> > >                         Exit For
> > >                     End If
> > >                 Else
> > >                     Continue For
> > >                 End If
> > >             Next
> > >         Next
> > >         Console.Read()
> > >
> > >     End Sub
> > >
> > >
> > > mattdad***@gmail.com wrote:
> > > > Hi all,
> > > >
> > > > I've taken a couple of hours to read what is available, and I still
> > > > cannot figure out how to do a very simple task in vb .net...lol.
> > > >
> > > > All I need to do is read an xml file and parse out specific information
> > > > based on simple criteria. Let's use this as the xml file:
> > > >
> > > > <?xml version="1.0" encoding="utf-8" ?>
> > > > <sites>
> > > >   <siteData>
> > > >      <siteName>flux</siteName>
> > > >      <active>true</active>
> > > >      <email>m*@me.com</email>
> > > >      <email>y**@you.com</email>
> > > >   </siteData>
> > > >   <siteData>
> > > >      <siteName>capacitor</siteName>
> > > >      <active>false</active>
> > > >      <email>y*@yo.com</email>
> > > >   </siteData>
> > > >   <siteData>
> > > >     <siteName>scott</siteName>
> > > >     <active>true</active>
> > > >     <email>y**@you.com</email>
> > > >     <email>m*@me.com</email>
> > > >     <email>dup***@dupree.com</email>
> > > >   </siteData>
> > > > </sites>
> > > >
> > > > I need to read out the email addresses as strings based on the
> > > > siteName.
> > > >
> > > > I have seen at least 3 distinct ways to read xml.
> > > >
> > > > 1) Throw it into a dataset with DATASET.READXML
> > > >
> > > > 2) Use a xmldatareader  XMLREADER.CREATE(BLAH,BLAH)
> > > >
> > > > 3) Instantiate on xmldocument and load the file in  myXmlDoc as new
> > > > XmlDocument / myXmlDoc.Load(blah)
> > > >
> > > > I can load the file and display it, but I am having trouble with the
> > > > syntax for reading out the value/innertext of one of the nodes based on
> > > > another node value. So here is what exactly I would like to do.
> > > >
> > > > Traverse the xml file to the <siteData> node that contains whatever
> > > > <siteName> I specify. Then I need to iterate through the <email> nodes
> > > > (each site may have from 1 to 3 so I won't know how many)
> > > >
> > > > Any help is greatly appreciated. Thanks!

One other thing is that values that would be properties of a node (in
this case active, for example) should be attributes and not nodes.  And
rather than call the nodes <sideData> they should probably just be
<site>.  And I agree with rowe that the email nodes should themselves
be children of a containing node for example:

<EMailAddresses>
    <EMailAddress>b***@blah.com</EMailAddress>
</EMailAddresses>

If the xml were structured this way, you could use an XPath query to
return the exact EMailAddresses node you needed and then iterate
through each address in that node:

Considering this xml:

<?xml version="1.0" encoding="utf-8" ?>
<sites>
  <site Name="Flux" Active="True">
     <EMailAddresses>
         <EMailAddress>m*@me.com</email>
         <EMailAddress>y**@you.com</email>
     </EMailAddresses>
  </site>
  <site Name="Capacitor" Active="False">
     <EMailAddresses>
         <EMailAddress>y*@yo.com</EMailAddress>
     </EMailAddresses>
  </site>
  <site Name="Scott" Active="True">
     <EMailAddresses>
        <EMailAddress>y**@you.com</EMailAddress>
        <EMailAddress>m*@me.com</EMailAddress>
        <EMailAddress>dup***@dupree.com</EMailAddress>
     </EMailAddresses>
  </site>
</sites>


<AirCode>

'Load the xml file
Dim xDoc As New XmlDocument
xDoc.Load("xmlfilename.xml")

'Now select the EMailAddresses node for the Site with Name = "Flux"
Dim xNode As XmlNode
xNode = xDoc.SelectSingleNode("\Site[@Name='Flux']\EMailAddresses")

If xNode IsNot Nothing Then
    'Iterate through xNodes children here
    For Each x As XmlNode In xNode.ChildNodes
        Console.WriteLine(x.InnerText)
    Next
End If

</AirCode>

You can still use XPath even if the xml is not formatted the way I show
it, but I think, at least, the email addresses should be in a container
node.

Hope this helps a little

Chris