none
XML Reading

    Question

  • I am trying to get the value for the line in the XML file below, the only one I want is the first number the 31. I tried using this code but it recalled a different value. How do I retrieve only that first value in the line?

    Imports System.IO
    Imports System.Xml
    
    Public Class Crop
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            While (document.Read())
                If (type = XmlNodeType.Element) Then
                    If (document.Name = "value") Then
                        Report.MinTemp.Text = document.ReadInnerXml.ToString()
                    End If
                End If
            End While
    End Sub
    End Class
    

    parameters applicable-location="point1">
    <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Minimum Temperature</name>
    <value>31</value>  <---- This is the value I wanted
    <value>41</value>
    <value>35</value>
    <value>32</value>
    <value>45</value>
    <value>36</value>
    <value>42</value>
    </temperature>


    Regards, Carter Humphreys

    Sunday, April 06, 2014 9:19 PM

Answers

  • Carter,

    Give this some thought and work with it a bit:

    Option Strict On Option Explicit On Imports <xmlns:alias="http://www.w3.org/2001/XMLSchema"> Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim xDoc = XElement.Load("http://forecast.weather.gov/MapClick.php?lat=42.51&lon=-76.94&unit=0&lg=english&FcstType=dwml") For Each pInfo As XElement In xDoc...<parameters> For Each tInfo As XElement In pInfo...<temperature> If tInfo.@type = "minimum" Then For Each vInfo As XElement In tInfo...<value> ' Carter: Hover your mouse over "vInfo" ' above. Stop Next End If Next Next End Sub End Class



    Please call me Frank :)

    • Marked as answer by CarterJH Sunday, April 06, 2014 11:34 PM
    Sunday, April 06, 2014 10:18 PM

All replies

  • You need to exit out of the loop after you read the first value. What you have keeps looping. What is it, Exit While? after Report.MinTemp.Text =
    Sunday, April 06, 2014 9:29 PM
  • You need to exit out of the loop after you read the first value. What you have keeps looping. What is it, Exit While? after Report.MinTemp.Text =

    There is an end while, the part I cant figure out is the value I want is s subsection under another heading.

    XML file: http://forecast.weather.gov/MapClick.php?lat=42.51&lon=-76.94&unit=0&lg=english&FcstType=dwml


    Regards, Carter Humphreys

    Sunday, April 06, 2014 9:32 PM
  • I am trying to get the value for the line in the XML file below, the only one I want is the first number the 31. I tried using this code but it recalled a different value. How do I retrieve only that first value in the line?

    Imports System.IO
    Imports System.Xml
    
    Public Class Crop
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            While (document.Read())
                If (type = XmlNodeType.Element) Then
                    If (document.Name = "value") Then
                        Report.MinTemp.Text = document.ReadInnerXml.ToString()
                    End If
                End If
            End While
    End Sub
    End Class

    parameters applicable-location="point1">
    <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Minimum Temperature</name>
    <value>31</value>  <---- This is the value I wanted
    <value>41</value>
    <value>35</value>
    <value>32</value>
    <value>45</value>
    <value>36</value>
    <value>42</value>
    </temperature>


    Regards, Carter Humphreys

    Carter,

    Consider using LINQ-To-XML to do what you want.


    Please call me Frank :)

    Sunday, April 06, 2014 9:38 PM
  • I am trying to get the value for the line in the XML file below, the only one I want is the first number the 31. I tried using this code but it recalled a different value. How do I retrieve only that first value in the line?

    Imports System.IO
    Imports System.Xml
    
    Public Class Crop
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            While (document.Read())
                If (type = XmlNodeType.Element) Then
                    If (document.Name = "value") Then
                        Report.MinTemp.Text = document.ReadInnerXml.ToString()
                    End If
                End If
            End While
    End Sub
    End Class

    parameters applicable-location="point1">
    <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Minimum Temperature</name>
    <value>31</value>  <---- This is the value I wanted
    <value>41</value>
    <value>35</value>
    <value>32</value>
    <value>45</value>
    <value>36</value>
    <value>42</value>
    </temperature>


    Regards, Carter Humphreys

    Carter,

    Consider using LINQ-To-XML to do what you want.


    Please call me Frank :)

    I don't understand how to do it?

    Regards, Carter Humphreys

    Sunday, April 06, 2014 9:52 PM
  • I don't understand how to do it?

    The first thing to recognize is that you have to evaluate the entire XML file, not just a small portion of it. When you do, notice the very first line - that establishes a namespace.

    Once you handle the namespace, the rest isn't so bad.

    If you don't have a suitable answer tomorrow then I'll see what I can do with it, but LINQ-To-XML would be the direction that I'd go. Obviously it's not the only way though. :)


    Please call me Frank :)


    Sunday, April 06, 2014 10:00 PM
  • Carter,

    Give this some thought and work with it a bit:

    Option Strict On Option Explicit On Imports <xmlns:alias="http://www.w3.org/2001/XMLSchema"> Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim xDoc = XElement.Load("http://forecast.weather.gov/MapClick.php?lat=42.51&lon=-76.94&unit=0&lg=english&FcstType=dwml") For Each pInfo As XElement In xDoc...<parameters> For Each tInfo As XElement In pInfo...<temperature> If tInfo.@type = "minimum" Then For Each vInfo As XElement In tInfo...<value> ' Carter: Hover your mouse over "vInfo" ' above. Stop Next End If Next Next End Sub End Class



    Please call me Frank :)

    • Marked as answer by CarterJH Sunday, April 06, 2014 11:34 PM
    Sunday, April 06, 2014 10:18 PM
  • You need to exit out of the loop after you read the first value. What you have keeps looping. What is it, Exit While? after Report.MinTemp.Text =

    There is an end while, the part I cant figure out is the value I want is s subsection under another heading.

    XML file: http://forecast.weather.gov/MapClick.php?lat=42.51&lon=-76.94&unit=0&lg=english&FcstType=dwml


    Regards, Carter Humphreys

    Is there a specific reason you are using the XML file? Kevin showed a great example of using the weather web service at this link maybe that's easier?

    Sunday, April 06, 2014 10:38 PM
  • Is there a specific reason you are using the XML file? Kevin showed a great example of using the weather web service at this link maybe that's easier?

    Didn't like my LINQ-To-XML example Tom?

    ;-)

    I also have one here, (a web-based API, not a service) but it's not what Carter asked about, so ...


    Please call me Frank :)

    Sunday, April 06, 2014 10:58 PM
  • I will try what you have there Frank once I get back to my computer. Tom, I am using the National Weather Service XML file, I just prefer there forecasts so that is what I decided to use to get the data

    Regards, Carter Humphreys

    Sunday, April 06, 2014 11:38 PM
  • I will try what you have there Frank once I get back to my computer. Tom, I am using the National Weather Service XML file, I just prefer there forecasts so that is what I decided to use to get the data

    Regards, Carter Humphreys

    I very specifically left it "open" for you to work with. You still have to get at what you want which is the value itself:

    Dim minValue As Double = Double.Parse(vInfo.Value)

    That's one way. CDbl (which is actually casting it to a type double) is another.

    *****

    Obviously, or I hope it's obvious, all of that needs to be inside a Try/Catch block.

    *****

    Now the next challenge is to ONLY get that one - and Tom suggested one way but it's not quite enough.

    Think it through some here -- you're three tiers inside a For Each loop. There are several ways you can handle that, and I'd like for you to experiment with whatever ways you think are appropriate, but consider that you need to tell the code "ok, I've got what I need, I'm done now" and how you'd do that.

    *****

    Lastly, you might also consider getting ALL of the data - if it's of use to you.

    It's a lot, sure, but why not? If you remember from a few weeks back, use the concept of a class, build a function which will return an instance of the class, and you can pretty quickly have ALL of what's in that XML file. It won't take as long to get it all as you might think. Once loaded into memory, LINQ-To-XML makes short work of the rest of it.

    I'm glad it helped. :)


    Please call me Frank :)

    Sunday, April 06, 2014 11:48 PM