Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / November 2006

Tip: Looking for answers? Try searching our database.

another vb .net xml question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mattdaddym@gmail.com - 21 Nov 2006 15:45 GMT
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>me@me.com</email>
    <email>you@you.com</email>
 </siteData>
 <siteData>
    <siteName>capacitor</siteName>
    <active>false</active>
    <email>yo@yo.com</email>
 </siteData>
 <siteData>
   <siteName>scott</siteName>
   <active>true</active>
   <email>you@you.com</email>
   <email>me@me.com</email>
   <email>dupree@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!
rowe_newsgroups - 21 Nov 2006 16:52 GMT
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

> Hi all,
>
[quoted text clipped - 47 lines]
>
> Any help is greatly appreciated. Thanks!
mattdaddym@gmail.com - 21 Nov 2006 19:14 GMT
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!
> 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
[quoted text clipped - 93 lines]
> >
> > Any help is greatly appreciated. Thanks!
rowe_newsgroups - 21 Nov 2006 19:55 GMT
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

> 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
[quoted text clipped - 96 lines]
> > >
> > > Any help is greatly appreciated. Thanks!
Chris Dunaway - 21 Nov 2006 22:25 GMT
> 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
[quoted text clipped - 104 lines]
> > > >
> > > > 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>blah@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>me@me.com</email>
        <EMailAddress>you@you.com</email>
    </EMailAddresses>
 </site>
 <site Name="Capacitor" Active="False">
    <EMailAddresses>
        <EMailAddress>yo@yo.com</EMailAddress>
    </EMailAddresses>
 </site>
 <site Name="Scott" Active="True">
    <EMailAddresses>
       <EMailAddress>you@you.com</EMailAddress>
       <EMailAddress>me@me.com</EMailAddress>
       <EMailAddress>dupree@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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.