I need help with XmlReader

ucchris77

Member
Joined
Jul 20, 2010
Messages
6
Programming Experience
Beginner
I am trying to create a function that uses XmlReader to get a single value from an xml file. This function is passed two parameters:

chartName: which is the name of the ChartName attribute for the <Chart> node that you are looking for.

elementName: which is the name of the element you are looking for.

For example if I wanted to get the value of goal from the Astenteeism chart I would do
Dim myString As String = getStuff("Goal","Absenteeism")
This should return 0.5 (see xml below)

But the problem is that it will return 0.8:confused: For some reason it is grabbing the goal tag value from whatever goal tag is first. I'm not sure why?

I tried to give as much info as I could let me know if any details are confusing. Thanks for your help.

here's my code.

VB.NET:
Private Function getStuff(ByVal chartName As String, ByVal elementName As String) As String

        Dim settings As New XmlReaderSettings
        settings.IgnoreComments = True
        settings.IgnoreWhitespace = True

        Dim myXMLFile As XmlReader = XmlReader.Create("C:\Documents and Settings\honeymoon\My Documents\2010_Config.xml", settings)

        Dim locationNameString As String
        locationNameString = Nothing

        myXMLFile.MoveToAttribute(chartName)
        myXMLFile.MoveToElement()
        myXMLFile.ReadToFollowing(elementName)
        locationNameString = myXMLFile.ReadElementContentAsString

        Return locationNameString

    End Function


Here's the xml file.

VB.NET:
<?xml version="1.0"?> 
	<Excel>
		<Foo>
			<Chart ChartName = "Operational Availability">
				<Location>2010_OperationalAvailability.xlsm</Location>
				<Goal>0.8</Goal>
			</Chart>
			<Chart ChartName = "Absenteeism">
				<Location>2010_OperationalAvailability.xlsm</Location>
				<Goal>0.5</Goal>
			</Chart>
		</Foo>
	</Excel>
 
I never get used to all those move operations either, I find XPath expressions much easier to work with, example:
VB.NET:
Dim reader As New Xml.XPath.XPathDocument("2010_Config.xml")
Dim nav As Xml.XPath.XPathNavigator = reader.CreateNavigator
Dim xpath As String = String.Format("//Chart[@ChartName='{0}']/{1}", chartName, elementName)
Return nav.SelectSingleNode(xpath).Value
 
I found XPath expressions easier to work with as well.

Here's an alternate example using LINQ which I find easier to work with than XPath.

VB.NET:
        Dim elementName As String = "Chart"
        Dim attributeName As String = "ChartName"
        Dim searchTerm As String = "Absenteeism"

        Dim xDoc = XDocument.Load("C:\Temp\chart.xml")
        Dim theChartNode = (From node In xDoc.Descendants(elementName)
                   Where node.Attribute(attributeName).Value = searchTerm).FirstOrDefault

        If theChartNode IsNot Nothing Then
            MessageBox.Show(theChartNode.<Goal>.Value)
        Else
            MessageBox.Show(searchTerm & " not found in file")
        End If
 
John I tried your suggestion and it worked perfectly. Thank you.:) Matt I will have to try LINQ and see if I like that too. Thanks for your help guys.
 
There's absolutely nothing wrong with using XPath. I still have a lot of .NET 2.0 programs running that are littered with XPath expressions and they work fantastically.

The difference for me is that I don't use XML every day while I do use SQL all day every day. Since the LINQ statement syntax is similar it takes me less time to wrap my head around the next time I have to come back to it.

The point is that there are multiple ways to skin a cat; pick one that works for you, the more alternatives you have available the better the chance you're going to find one that makes sense.
 
Back
Top