在Windows(10)通用应用程序中读取vb.net中的XML over HTTP

时间:2016-04-27 21:20:51

标签: xml vb.net http win-universal-app

我正在弄清楚如何在Windows(10)通用应用程序中在vb.net中读取XML over HTTP。我已经尝试了几种可能性,但我无法让它们中的任何一种工作。有人可以帮助我吗?

问候

编辑:我已经设法在XDocument中获取我的xml代码。任何人都知道如何进一步阅读并从中获取某些物品?

    Dim xml As String = String.Empty
    Dim url As New Uri(Convert.ToString((link)))
    Dim httpClient As New HttpClient()
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml")
    Dim response = Await httpClient.GetAsync(url)
    Using responseStream = Await response.Content.ReadAsStreamAsync()
        Using streamReader = New StreamReader(responseStream)
            xml = streamReader.ReadToEnd()
        End Using
    End Using
    Dim xDoc As XDocument = XDocument.Parse(xml)

1 个答案:

答案 0 :(得分:0)

既然你已经在XDocument中获取了你的xml代码,并且我没有你的xml,假设我们已经得到了如下的xml:

<Root>
    <Father>
        <Child1>data1</Child1>
        <Child2>data2</Child2>
    </Father>
</Root>

接下来我们需要阅读某个项目。我们可以使用XElement类读取节点。 例如,从Father

获取名为xDoc的节点
Dim xDoc As XDocument = XDocument.Parse(xml)    
Dim root As XElement = xDoc.Root
Dim Fathernode As XElement = root.Element("Father")

与子节点

相同
Dim childnode As XElement = root.Element("Father").Element("Child1")

这些将获得整个节点。如果您只想获取节点的值, 使用

Dim value As String= root.Element("Father").Element("Child1").Value

更多详情请参阅XElement Class