another vb .net xml question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mattdaddym@gmail.com

    #1

    another vb .net xml question

    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>flu x</siteName>
    <active>true</active>
    <email>me@me.co m</email>
    <email>you@you. com</email>
    </siteData>
    <siteData>
    <siteName>capac itor</siteName>
    <active>false </active>
    <email>yo@yo.co m</email>
    </siteData>
    <siteData>
    <siteName>scott </siteName>
    <active>true</active>
    <email>you@you. com</email>
    <email>me@me.co m</email>
    <email>dupree@d upree.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.CREAT E(BLAH,BLAH)

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

    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 <siteDatanode that contains whatever
    <siteNameI specify. Then I need to iterate through the <emailnodes
    (each site may have from 1 to 3 so I won't know how many)

    Any help is greatly appreciated. Thanks!

  • rowe_newsgroups

    #2
    Re: another vb .net xml question

    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:\Documen ts and
    Settings\srowe\ Desktop\test.xm l" ' your path to the xml file here
    Dim doc As New Xml.XmlDocument
    doc.Load(DDIR)
    Dim nlist As Xml.XmlNodeList =
    doc.GetElements ByTagName("site Data")
    For Each n As Xml.XmlNode In nlist
    For i As Integer = 0 To n.ChildNodes.Co unt - 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.WriteLi ne(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.WriteLi ne(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


    mattdaddym@gmai l.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>flu x</siteName>
    <active>true</active>
    <email>me@me.co m</email>
    <email>you@you. com</email>
    </siteData>
    <siteData>
    <siteName>capac itor</siteName>
    <active>false </active>
    <email>yo@yo.co m</email>
    </siteData>
    <siteData>
    <siteName>scott </siteName>
    <active>true</active>
    <email>you@you. com</email>
    <email>me@me.co m</email>
    <email>dupree@d upree.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.CREAT E(BLAH,BLAH)
    >
    3) Instantiate on xmldocument and load the file in myXmlDoc as new
    XmlDocument / myXmlDoc.Load(b lah)
    >
    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 <siteDatanode that contains whatever
    <siteNameI specify. Then I need to iterate through the <emailnodes
    (each site may have from 1 to 3 so I won't know how many)
    >
    Any help is greatly appreciated. Thanks!

    Comment

    • mattdaddym@gmail.com

      #3
      Re: another vb .net xml question

      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:\Documen ts and
      Settings\srowe\ Desktop\test.xm l" ' your path to the xml file here
      Dim doc As New Xml.XmlDocument
      doc.Load(DDIR)
      Dim nlist As Xml.XmlNodeList =
      doc.GetElements ByTagName("site Data")
      For Each n As Xml.XmlNode In nlist
      For i As Integer = 0 To n.ChildNodes.Co unt - 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.WriteLi ne(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.WriteLi ne(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
      >
      >
      mattdaddym@gmai l.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>flu x</siteName>
      <active>true</active>
      <email>me@me.co m</email>
      <email>you@you. com</email>
      </siteData>
      <siteData>
      <siteName>capac itor</siteName>
      <active>false </active>
      <email>yo@yo.co m</email>
      </siteData>
      <siteData>
      <siteName>scott </siteName>
      <active>true</active>
      <email>you@you. com</email>
      <email>me@me.co m</email>
      <email>dupree@d upree.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.CREAT E(BLAH,BLAH)

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

      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 <siteDatanode that contains whatever
      <siteNameI specify. Then I need to iterate through the <emailnodes
      (each site may have from 1 to 3 so I won't know how many)

      Any help is greatly appreciated. Thanks!

      Comment

      • rowe_newsgroups

        #4
        Re: another vb .net xml question

        Are you stuck with the way that XML file is laid out or could you
        reformat it? If, for example, the <emailnodes where children of the
        <siteNamenode s I could write a much better algorithm.

        Thanks,

        Seth Rowe


        mattdaddym@gmai l.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:\Documen ts and
        Settings\srowe\ Desktop\test.xm l" ' your path to the xml file here
        Dim doc As New Xml.XmlDocument
        doc.Load(DDIR)
        Dim nlist As Xml.XmlNodeList =
        doc.GetElements ByTagName("site Data")
        For Each n As Xml.XmlNode In nlist
        For i As Integer = 0 To n.ChildNodes.Co unt - 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.WriteLi ne(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.WriteLi ne(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


        mattdaddym@gmai l.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>flu x</siteName>
        <active>true</active>
        <email>me@me.co m</email>
        <email>you@you. com</email>
        </siteData>
        <siteData>
        <siteName>capac itor</siteName>
        <active>false </active>
        <email>yo@yo.co m</email>
        </siteData>
        <siteData>
        <siteName>scott </siteName>
        <active>true</active>
        <email>you@you. com</email>
        <email>me@me.co m</email>
        <email>dupree@d upree.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.CREAT E(BLAH,BLAH)
        >
        3) Instantiate on xmldocument and load the file in myXmlDoc as new
        XmlDocument / myXmlDoc.Load(b lah)
        >
        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 <siteDatanode that contains whatever
        <siteNameI specify. Then I need to iterate through the <emailnodes
        (each site may have from 1 to 3 so I won't know how many)
        >
        Any help is greatly appreciated. Thanks!

        Comment

        • Chris Dunaway

          #5
          Re: another vb .net xml question

          rowe_newsgroups wrote:
          Are you stuck with the way that XML file is laid out or could you
          reformat it? If, for example, the <emailnodes where children of the
          <siteNamenode s I could write a much better algorithm.
          >
          Thanks,
          >
          Seth Rowe
          >
          >
          mattdaddym@gmai l.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:\Documen ts and
          Settings\srowe\ Desktop\test.xm l" ' your path to the xml file here
          Dim doc As New Xml.XmlDocument
          doc.Load(DDIR)
          Dim nlist As Xml.XmlNodeList =
          doc.GetElements ByTagName("site Data")
          For Each n As Xml.XmlNode In nlist
          For i As Integer = 0 To n.ChildNodes.Co unt - 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.WriteLi ne(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.WriteLi ne(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
          >
          >
          mattdaddym@gmai l.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>flu x</siteName>
          <active>true</active>
          <email>me@me.co m</email>
          <email>you@you. com</email>
          </siteData>
          <siteData>
          <siteName>capac itor</siteName>
          <active>false </active>
          <email>yo@yo.co m</email>
          </siteData>
          <siteData>
          <siteName>scott </siteName>
          <active>true</active>
          <email>you@you. com</email>
          <email>me@me.co m</email>
          <email>dupree@d upree.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.CREAT E(BLAH,BLAH)

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

          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 <siteDatanode that contains whatever
          <siteNameI specify. Then I need to iterate through the <emailnodes
          (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 <sideDatathey 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 lah@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 e@me.com</email>
          <EMailAddress>y ou@you.com</email>
          </EMailAddresses>
          </site>
          <site Name="Capacitor " Active="False">
          <EMailAddresses >
          <EMailAddress>y o@yo.com</EMailAddress>
          </EMailAddresses>
          </site>
          <site Name="Scott" Active="True">
          <EMailAddresses >
          <EMailAddress>y ou@you.com</EMailAddress>
          <EMailAddress>m e@me.com</EMailAddress>
          <EMailAddress>d upree@dupree.co m</EMailAddress>
          </EMailAddresses>
          </site>
          </sites>


          <AirCode>

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

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

          If xNode IsNot Nothing Then
          'Iterate through xNodes children here
          For Each x As XmlNode In xNode.ChildNode s
          Console.WriteLi ne(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

          Comment

          Working...