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 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
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.
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).
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:
Do While document.Read()
Dim type = document.NodeType
If type = XmlNodeType.Element And document.Name = "current_conditions" Then
Dim section = document.ReadSubtree
Do While section.Read
If section.Name = "condition" Then
'this is a down and dirty way to parse the entry but it's easier to imitate for other entries