VB.NET Reading and Decoding Specific XML Contents and Attributes from a Website

blinkor12 101 Reputation points
2021-04-05T01:21:55.663+00:00

I'm trying to read an XML and its content/attributes from a website. I'm having trouble reading the XML itself.

For example, the XML at my website looks like this. Any number of items is contained within a record grouped by date:

<weatherReport>
<record date="2021y04m05d">
<item time="060452" conditions="sun" url="https://example.com/weatherAPI/detailedReport/1.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060453" conditions="sun" url="https://example.com/weatherAPI/detailedReport/2.xml">Currently Sunny. High 34 Degrees. Low 24 Degrees.</item>
<item time="060454" conditions="sun" url="https://example.com/weatherAPI/detailedReport/3.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060951" conditions="rain" url="https://example.com/weatherAPI/detailedReport/4.xml">Currently Rainy. High 41 Degrees. Low 36 Degrees.</item>
<item time="060952" conditions="rain" url="https://example.com/weatherAPI/detailedReport/5.xml">Currently Rainy. High 42 Degrees. Low 40 Degrees.</item>
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
<record date="2021y04m04d">
<item time="060452" conditions="sun" url="https://example.com/weatherAPI/detailedReport/1.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060453" conditions="sun" url="https://example.com/weatherAPI/detailedReport/2.xml">Currently Sunny. High 34 Degrees. Low 24 Degrees.</item>

</record>
<record date="2021y04m03d">
<item time="060952" conditions="rain" url="https://example.com/weatherAPI/detailedReport/5.xml">Currently Rainy. High 42 Degrees. Low 40 Degrees.</item>
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
<record date="2021y04m02d">
<item time="060452" conditions="sun" url="https://example.com/weatherAPI/detailedReport/1.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060453" conditions="sun" url="https://example.com/weatherAPI/detailedReport/2.xml">Currently Sunny. High 34 Degrees. Low 24 Degrees.</item>
<item time="060454" conditions="sun" url="https://example.com/weatherAPI/detailedReport/3.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060951" conditions="rain" url="https://example.com/weatherAPI/detailedReport/4.xml">Currently Rainy. High 41 Degrees. Low 36 Degrees.</item>
<item time="060952" conditions="rain" url="https://example.com/weatherAPI/detailedReport/5.xml">Currently Rainy. High 42 Degrees. Low 40 Degrees.</item>
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
<record date="2021y04m01d">
<item time="060452" conditions="sun" url="https://example.com/weatherAPI/detailedReport/1.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060453" conditions="sun" url="https://example.com/weatherAPI/detailedReport/2.xml">Currently Sunny. High 34 Degrees. Low 24 Degrees.</item>
<item time="060454" conditions="sun" url="https://example.com/weatherAPI/detailedReport/3.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060951" conditions="rain" url="https://example.com/weatherAPI/detailedReport/4.xml">Currently Rainy. High 41 Degrees. Low 36 Degrees.</item>
<item time="060952" conditions="rain" url="https://example.com/weatherAPI/detailedReport/5.xml">Currently Rainy. High 42 Degrees. Low 40 Degrees.</item>
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
<record date="2021y03m31d">
<item time="060452" conditions="sun" url="https://example.com/weatherAPI/detailedReport/1.xml">Currently Sunny. High 36 Degrees. Low 24 Degrees.</item>
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
<record date="2021y03m30d">
<item time="060953" conditions="rain" url="https://example.com/weatherAPI/detailedReport/6.xml">Currently Rainy. High 49 Degrees. Low 39 Degrees.</item>
</record>
</weatherReport>

I have several questions about how to retrieve this data. I'll ask them all in one question so I won't have to make any additional questions again about this topic.

First, how would I get the value of the record's date (<record date="">) when supplied the record's number in the xml (e.x the First Record)?

Second, how would I get the value of item time/conditions (attribute) within each item when given the record number (e.g the first record) and the item number within the record (e.g the second item in the first record record).

Third, how would I get the actual contents of each item when provided the record number and the item number (such as "Currently Sunny. High 36 Degrees. Low 24 Degrees.")?

So far, I've tried this code to download some XML from my website:

Dim address As String = "https://example.com/weatherAPI/collectiveReport.xml"
        Dim client As WebClient = New WebClient()
        Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
        Dim rawXML As String = reader.ReadToEnd()

and It works fetching the raw XML. However, this does not work with any of the numerous examples provided online because they all read XML from a file saved on the user's computer. Since I make requests very quickly, I cannot download the XML online to a file on the PC because it is not efficient saving many XML files to the PC. I haven't found any good sources solving these issues.

Is there any way I can solve these three problems?

Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-04-05T05:30:05.557+00:00

    Hi @blinkor12 ,

    You can try to use XDocument and linq to get the xml information you wanted.

    Here is a VB.NET code example you can refer to.

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim text = File.ReadAllText("E:\\test.xml")  
            Dim doc As XDocument = XDocument.Parse(text)  
            Dim result1 = doc.Descendants("record").First()  
            RichTextBox1.AppendText(result1.Attribute("date").Value)                                    //First Question  
            RichTextBox1.AppendText(Environment.NewLine + "*********************" + Environment.NewLine)               
            Dim result2 = From t In doc.Descendants("record")  
                          Where t.Attribute("date").Value = "2021y04m05d"  
                          Select t  
    
            For Each item In result2.Descendants("item")  
                RichTextBox1.AppendText(item.Attribute("time").Value + "   " + item.Attribute("conditions").Value + Environment.NewLine)   //Second Question  
            Next  
            RichTextBox1.AppendText(Environment.NewLine + "*********************" + Environment.NewLine)  
            Dim result3 = From g In doc.Descendants("record")  
                          Where g.Attribute("date").Value = "2021y04m05d"  
    
            For Each item In result3.Descendants("item")  
                If item.Attribute("time").Value = "060453" Then  
                    RichTextBox1.AppendText(item.Value)                                    //Third question  
                End If  
    
            Next  
    
    
        End Sub  
    

    Result:

    84356-image.png

    If you have problem in the code, please feel free to let me know.

    Best Regards,
    Jack


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.