vb.net - 阅读Xml Web响应

时间:2014-12-04 13:44:28

标签: xml vb.net parsing web response

我需要从vb.net的响应网页中读取一些值

当我去网址= 192.168.1.254/?custom=1&cmd=3014 我收到了这个回复:

    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>

我需要一种获取cmd id和值的方法。所以我可以知道单一功能的状态。

例如:

3003 - 0我知道旋转功能已关闭 3004 - 1我知道函数Date已打开 3008 - 4我将知道使用什么分辨率

Etc等。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

一些指示可以帮助您入门。这显示了如何从服务器下载XML作为字符串,然后使用LINQ to XML将其解析为匿名类型列表。您需要计算出适当的导入和选项(可能需要Option Infer On)。您可以使用相同的DownloadString方法向服务器发出其他GET个请求。如果这是在某种UI中,您可能需要查看WebClient.DownloadStringTaskAsync,以避免阻止。

' 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