Results 1 to 5 of 5

Thread: I need help with XmlReader

  1. #1
    ucchris77 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    6
    Reputation
    0

    I need help with XmlReader

    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.

    Code:
    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.

    Code:
    <?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>

  2. #2
    JohnH's Avatar
    JohnH is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,200
    Reputation
    2369
    I never get used to all those move operations either, I find XPath expressions much easier to work with, example:
    Code:
    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

  3. #3
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    571
    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.

    Code:
            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

  4. #4
    ucchris77 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    6
    Reputation
    0

    Smile

    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.

  5. #5
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    571
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking