Search code examples
xmlvb.netparsingwebresponse

vb.net - Read Xml web response


I need to read the some values from a response web page in vb.net

When i go to the url= 192.168.1.254/?custom=1&cmd=3014 i get this response:

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Function>
<Cmd>2002</Cmd>
<Status>0</Status>
<Cmd>2003</Cmd>
<Status>0</Status>
<Cmd>2004</Cmd>
<Status>0</Status>
<Cmd>2006</Cmd>
<Status>0</Status>
<Cmd>2007</Cmd>
<Status>1</Status>
<Cmd>2008</Cmd>
<Status>0</Status>
<Cmd>2010</Cmd>
<Status>3</Status>
<Cmd>1004</Cmd>
<Status>0</Status>
<Cmd>1002</Cmd>
<Status>4</Status>
<Cmd>1005</Cmd>
<Status>0</Status>
<Cmd>1006</Cmd>
<Status>1</Status>
<Cmd>1007</Cmd>
<Status>0</Status>
<Cmd>1008</Cmd>
<Status>0</Status>
<Cmd>1009</Cmd>
<Status>0</Status>
<Cmd>2005</Cmd>
<Status>6</Status>
<Cmd>1011</Cmd>
<Status>0</Status>
<Cmd>3025</Cmd>
<Status>0</Status>
<Cmd>3026</Cmd>
<Status>1</Status>
<Cmd>3011</Cmd>
<Status>1</Status>
<Cmd>3010</Cmd>
<Status>1</Status>
<Cmd>3007</Cmd>
<Status>0</Status>
<Cmd>3003</Cmd>
<Status>1</Status>
<Cmd>3004</Cmd>
<Status>1</Status>
<Cmd>3008</Cmd>
<Status>0</Status>
</Function>

I need a way to get the cmd id and the value. So i can know the status of the single functions.

Ex:

3003 - 0 i'll know that the function Rotate is off 3004 - 1 i'll know that the function Date is on 3008 - 4 i'll know what resolution is using

Etc etc.

How can i do that ?


Solution

  • Some pointers to get you started. This shows how to download the XML from the server as a string and then parse it, using LINQ to XML, into a list of anonymous types. You'll need to work out the appropriate imports and options (Option Infer On is probably required). You can use the same DownloadString method to make other GET requests to the server. You may want to look at WebClient.DownloadStringTaskAsync if this is in some sort of UI, to avoid blocking.

    ' Download the XML from the server
    Dim wc = New WebClient()
    Dim xmlString As String = wc.DownloadString("http://192.168.1.254/?custom=1&cmd=3014")
    
    ' Parse the XML into something more usable
    Dim xml = XElement.Parse(xmlString)
    Dim cmdStatus =
        From c In xml...<Cmd>
        Let s = DirectCast(c.NextNode, XElement)
        Select Cmd = c.Value, Status = s.Value
    
    ' Dump the value to the console - not sure what you want to do here
    For Each cs In cmdStatus
        Console.WriteLine("Cmd: {0}, Status: {1}", cs.Cmd, cs.Status)
    Next