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
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.
Here's the xml file.
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
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>