Results 1 to 8 of 8

Thread: Reading XML file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    Arrow Reading XML file

    Hey guys, I got a problem...
    So I managed to read an XML file by using a simple code, but the value I was trying to read was a string.
    Now I need to read an integer, but it's formatted like so:
    Code:
    <temp_c data="17"/>
    So now how do I get the value 17, from that?
    This code doesn't seem to work:
    Code:
    If (document.Name = "temp_c data=") Then
                            Label4.Text = document.ReadInnerXml.ToString()
                        End If
    PLEASE ANSWER!!!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading XML file

    Please post the code (in full) that enabled you to read the string. What exactly is 'document' in this sample?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    Re: Reading XML file

    Quote Originally Posted by dunfiddlin View Post
    Please post the code (in full) that enabled you to read the string. What exactly is 'document' in this sample?
    Code:
    If (System.IO.File.Exists("C:\Users\Admin\Downloads\Chrome Downloads\api.xml")) Then
    
                Dim document As XmlReader = New XmlTextReader("C:\Users\Admin\Downloads\Chrome Downloads\api.xml")
    
                While (document.Read())
    
                    Dim type = document.NodeType
    
                    If (type = XmlNodeType.Element) Then
    
                        If (document.Name = "current_conditions") Then
    
                            Label3.Text = document.ReadInnerXml.ToString()
                            If Label3.Text.Contains("Cloudy") = True Then
                                Label2.Text = "Cloudy"
                                PictureBox7.Image = My.Resources.Status_weather_few_clouds_icon
                            End If
                        End If
                        If (document.Name = "temp_c data=") Then
                            Label4.Text = document.ReadInnerXml.ToString()
                        End If
                    End If
                End While
            End If

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading XML file

    VB Code:
    1. If (document.Name = "temp_c") Then
    2.        Label1.Text = String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(document.ReadOuterXml.ToString(), "[^\d]"))
    3.                 End If

    Note that the name does not include 'data=', and you're interested in the OuterXML, ie. what surrounds the name. The Regex is just the neatest way of extracting numbers from a string. Please note that it only works for integers and it will fail if there are numbers elsewhere than in the value (temp_c1 data="17" would return 117 for example). There are several other ways to go about the parsing in such circumstances, of course.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    Re: Reading XML file

    Quote Originally Posted by dunfiddlin View Post
    VB Code:
    1. If (document.Name = "temp_c") Then
    2.        Label1.Text = String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(document.ReadOuterXml.ToString(), "[^\d]"))
    3.                 End If

    Note that the name does not include 'data=', and you're interested in the OuterXML, ie. what surrounds the name. The Regex is just the neatest way of extracting numbers from a string. Please note that it only works for integers and it will fail if there are numbers elsewhere than in the value (temp_c1 data="17" would return 117 for example). There are several other ways to go about the parsing in such circumstances, of course.
    Nope, it doesn't seem to work... the text remains as it is... (i obviously changed Label1 to Label4, but still doesn't work).

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading XML file

    What do you mean 'remains as it is'? If it doesn't work then there isn't any text.

    Please attach a copy of the full XML file.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    Re: Reading XML file

    Quote Originally Posted by dunfiddlin View Post
    What do you mean 'remains as it is'? If it doesn't work then there isn't any text.

    Please attach a copy of the full XML file.
    I attached it.
    The part I'm most interested in is this one:
    Code:
    <current_conditions><condition data="Partly Cloudy"/><temp_f data="63"/><temp_c data="17"/><humidity data="Humidity: 52%"/><icon data="/ig/images/weather/partly_cloudy.gif"/><wind_condition data="Wind: W at 10 mph"/></current_conditions>
    Attached Files Attached Files

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading XML file

    Ok, got it now. As you were set up you were reading past the second category and then attempting to recover it in retrospect. I've revised it so that it reads the major nodes to find the section you're interested in and then starts a second reader to run through the individual values in that section.

    VB Code:
    1. Do While document.Read()
    2.  
    3.             Dim type = document.NodeType
    4.  
    5.             If type = XmlNodeType.Element And document.Name = "current_conditions" Then
    6.                 Dim section = document.ReadSubtree
    7.                 Do While section.Read
    8.                     If section.Name = "condition" Then
    9.  
    10. 'this is a down and dirty way to parse the entry but it's easier to imitate for other entries
    11.                         Label2.Text = section.ReadOuterXml.Substring(17)
    12.                         Label2.Text = Label2.Text.Substring(0, Label2.Text.Length - 4)
    13.                     End If
    14.  
    15.                     If section.Name = "temp_c" Then
    16.                         Label4.Text = String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(section.ReadOuterXml.ToString(), "[^\d]"))
    17.                     End If
    18.  
    19.                 Loop
    20.             End If
    21.         Loop

Posting Permissions

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



Click Here to Expand Forum to Full Width