another vb .net xml question

M

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>[email protected]</email>
<email>[email protected]</email>
</siteData>
<siteData>
<siteName>capacitor</siteName>
<active>false</active>
<email>[email protected]</email>
</siteData>
<siteData>
<siteName>scott</siteName>
<active>true</active>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</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!
 
R

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
 
M

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!
 
R

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
 
C

Chris Dunaway

rowe_newsgroups said:
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

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>[email protected]</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>[email protected]</email>
<EMailAddress>[email protected]</email>
</EMailAddresses>
</site>
<site Name="Capacitor" Active="False">
<EMailAddresses>
<EMailAddress>[email protected]</EMailAddress>
</EMailAddresses>
</site>
<site Name="Scott" Active="True">
<EMailAddresses>
<EMailAddress>[email protected]</EMailAddress>
<EMailAddress>[email protected]</EMailAddress>
<EMailAddress>[email protected]</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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Reading XML file getting error 3
Processing XML With C# and .NET 0
Processing XML With C# and .NET 1
Read XML Help. 9
Reading XML 1
Reading XML with VB.NET 1
Simple XML question 4
XML file and UTF8 4

Top