简体   繁体   English

如何从vb.net中的帖子解析xml响应

[英]How to parse an xml response from a post in vb.net

I am posting to a website to get data back. 我要发布到网站上以获取数据。 The site returns it as an xml. 该网站将其作为xml返回。 I am able to get the data into a string. 我能够将数据转换为字符串。 But what i really want to do is to have each item in the xml in a different string field. 但是我真正想做的是将xml中的每个项目都放在不同的字符串字段中。

Sub lookup(ByVal Source As Object, ByVal e As EventArgs)
  Dim wData As String
  wData = WRequest("http://PostToThisSite.com", "POST","str=31&Password=pn&UserID=Q&Postcode="+txtPcode.Text)       
  Response.Write(wData)
End Sub

Function WRequest(URL As String, method As String, POSTdata As String) As String
    Dim responseData As String = ""
      Try
        Dim hwrequest As Net.HttpWebRequest = Net.Webrequest.Create(URL)
        hwrequest.Accept = "*/*"
        hwrequest.AllowAutoRedirect = true
        hwrequest.UserAgent = "http_requester/0.1"
        hwrequest.Timeout = 60000
        hwrequest.Method = method
        If hwrequest.Method = "POST" Then
          hwrequest.ContentType = "application/x-www-form-urlencoded"
          Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
          Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
          hwrequest.ContentLength = postByteArray.Length
          Dim postStream As IO.Stream = hwrequest.GetRequestStream()
          postStream.Write(postByteArray, 0, postByteArray.Length)
          postStream.Close()
        End If
        Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
        If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
          Dim responseStream As IO.StreamReader = _
            New IO.StreamReader(hwresponse.GetResponseStream())
          responseData = responseStream.ReadToEnd()
        End If
        hwresponse.Close()
        Catch e As Exception
          responseData = "An error occurred: " & e.Message
        End Try
      Return responseData
End Function

The above code works and writes out a line... 上面的代码有效并写出一行...

Some Road City LU1 5QG 一些路城市LU1 5QG

The Xml being returned is .. 返回的Xml是..

<Address xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://site.co.uk/">
  <strOrganisation /> 
  <strProperty /> 
  <strStreet>Some Road</strStreet> 
  <strLocality /> 
  <strTown>City</strTown> 
  <strCounty /> 
  <strPostcode>LU1 5QG</strPostcode> 
  <strDPS /> 

I want to be able to split these fields and set them to different text boxes on the page...help? 我希望能够拆分这些字段并将它们设置为页面上的不同文本框...帮助?

Load the xml string into an XmlDocument and extract the values with XPath : 将xml字符串加载到XmlDocument中,并使用XPath提取值:

Dim doc = new XmlDocument()
doc.LoadXml(yourXmlString)
Dim nsm = new XmlNamespaceManager(doc.NameTable)
nsm.AddNamespace("a", "http://site.co.uk/")
txtStreet.Text = doc.SelectSingleNode("/a:Address/a:strStreet", nsm).InnerText

Here's a working snippet: 这是一个工作片段:

Dim doc = New XmlDocument()
doc.LoadXml("<Address xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://site.co.uk/""><strOrganisation /> <strProperty /> <strStreet>Some Road</strStreet> <strLocality /> <strTown>City</strTown> <strCounty /> <strPostcode>LU1 5QG</strPostcode><strDPS /></Address>")
Dim nsm = New XmlNamespaceManager(doc.NameTable)
nsm.AddNamespace("a", "http://site.co.uk/")
Dim streetValue = doc.SelectSingleNode("/a:Address/a:strStreet", nsm).InnerText

Some things to note: 有些事情需要注意:

  1. For XPath lookups, if your xml has a namespace you'll need to add it to an XmlNameSpaceManager created from your XmlDocument's NameTable . 对于XPath查找,如果xml具有名称空间,则需要将其添加到从XmlDocument的 NameTable创建的XmlNameSpaceManager中
  2. If you don't want to bother with that, you can walk the node collections manually via doc.ChildNodes[0].ChildNodes[0] etc. 如果您不想这样做,可以通过doc.ChildNodes[0].ChildNodes[0]等手动遍历节点集合。

Or try this, which is what I believe the author was asking. 或者尝试一下,这是我相信作者所要求的。

' Use XML Reader '使用XML Reader

        Dim xmlDoc = New XmlDocument
        Dim xmlNode As Xml.XmlNode
        xmlDoc.LoadXml(strResponse)

        xmlNode = xmlDoc.SelectSingleNode("//" + "strStreet")

        If Not xmlNode Is Nothing Then
            Dim Street = xmlNode.InnerText
        End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM