Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用VB.NET解析XML_.net_Vb.net_Xml Parsing - Fatal编程技术网

用VB.NET解析XML

用VB.NET解析XML,.net,vb.net,xml-parsing,.net,Vb.net,Xml Parsing,所以我在VB.NET中编写了这个程序(很好地使用了代码并对其进行了修改),它从帖子中获取信息。将该信息存储到值中。现在我试图解析这个值,但我无法解析。无论如何,任何人都可以提供一些我可以遵循的示例代码 注意:XML来自URL帖子。下面是一个片段: Imports System Imports System.IO Imports System.Net Imports System.Text Imports System.Xml Module Module1 Public Class We

所以我在VB.NET中编写了这个程序(很好地使用了代码并对其进行了修改),它从帖子中获取信息。将该信息存储到值中。现在我试图解析这个值,但我无法解析。无论如何,任何人都可以提供一些我可以遵循的示例代码

注意:XML来自URL帖子。下面是一个片段:

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml

Module Module1
    Public Class WebRequestPostExample
        Public Shared Sub Main()
            ' Create a request using a URL that can receive a post.
            Dim request As WebRequest = WebRequest.Create("https://quickvin.carfax.com/1 ")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
            ' Create POST data and convert it to a byte array.
            Dim postData As String

            postData = "<carfax-request>"
            postData = postData & "<license-plate>HSM2688</license-plate>"
            postData = postData & "<state>PA</state>"
            postData = postData & "<vin></vin>"
            postData = postData & "<product-data-id>5A5AE0DA8BC016CF</product-data-id>"
            postData = postData & "<location-id>CARFAX</location-id>"
            postData = postData & "</carfax-request>"

            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)


            Try
                Dim m_xmld As XmlDocument
                Dim m_nodelist As XmlNodeList
                Dim m_node As XmlNode
                'Create the XML Document
                m_xmld = New XmlDocument()
                'Load the Xml file
                XDocument.Load("https://quickvin.carfax.com/1") 'something keeps failing at this point
                'Get the list of name nodes 
                m_nodelist = m_xmld.SelectNodes("/request-info/")
                'Loop through the nodes
                For Each m_node In m_nodelist
                    'Get the Gender Attribute Value
                    Dim licenseplateAttribute = m_node.Attributes.GetNamedItem("license-plate").Value
                    'Get the firstName Element Value
                    Dim locationidValue = m_node.ChildNodes.Item(0).InnerText
                    'Get the lastName Element Value
                    Dim stateValue = m_node.ChildNodes.Item(1).InnerText
                    'Get the lastName Element Value
                    Dim vinValue = m_node.ChildNodes.Item(2).InnerText
                    'Write Result to the Console
                    Console.Write("Gender: " & licenseplateAttribute _
                      & " FirstName: " & locationidValue & " LastName: " _
                      & stateValue)
                    Console.Write(vbCrLf)
                Next
            Catch errorVariable As Exception
                'Error trapping
                Console.Write(errorVariable.ToString())
            End Try
            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub
    End Class
End Module
导入系统
导入System.IO
导入系统.Net
导入系统文本
导入System.Xml
模块1
公共类WebRequestPostExample
公共共享子主目录()
'使用可以接收帖子的URL创建请求。
Dim请求为WebRequest=WebRequest.Create(“https://quickvin.carfax.com/1 ")
'将请求的Method属性设置为POST。
request.Method=“POST”
'创建POST数据并将其转换为字节数组。
将postData设置为字符串
postData=“”
postData=postData&“HSM2688”
postData=postData和“PA”
postData=postData&“”
postData=postData&“5A5AE0DA8BC016CF”
postData=postData和“CARFAX”
postData=postData&“”
Dim byteArray As Byte()=Encoding.UTF8.GetBytes(postData)
'设置WebRequest的ContentType属性。
request.ContentType=“application/x-www-form-urlencoded”
'设置WebRequest的ContentLength属性。
request.ContentLength=byteArray.Length
'获取请求流。
Dim dataStream As Stream=request.GetRequestStream()
'将数据写入请求流。
dataStream.Write(byteArray,0,byteArray.Length)
'关闭流对象。
dataStream.Close()
“得到回应。
Dim响应为WebResponse=request.GetResponse()
'显示状态。
Console.WriteLine(CType(响应,HttpWebResponse).StatusDescription)
'获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream()
'使用StreamReader打开流以方便访问。
Dim读卡器作为新的StreamReader(数据流)
“阅读内容。
Dim responseFromServer As String=reader.ReadToEnd()
'显示内容。
Console.WriteLine(responseFromServer)
尝试
Dim m_xmld作为XmlDocument
Dim m_节点列表作为XmlNodeList
Dim m_节点作为XmlNode
'创建XML文档
m_xmld=新的XmlDocument()
'加载Xml文件
XDocument.Load(“https://quickvin.carfax.com/1“在这一点上,有些东西一直在失败
'获取名称节点的列表
m_nodelist=m_xmld.SelectNodes(“/request info/”)
'循环通过节点
对于m_节点列表中的每个m_节点
'获取性别属性值
Dim licenseplateAttribute=m_node.Attributes.GetNamedItem(“车牌”).Value
'获取firstName元素值
Dim locationidValue=m_node.ChildNodes.Item(0).InnerText
'获取lastName元素值
Dim stateValue=m_node.ChildNodes.Item(1).InnerText
'获取lastName元素值
Dim vinValue=m_node.ChildNodes.Item(2).InnerText
'将结果写入控制台
Console.Write(“性别:”&licenseplateAttribute_
&FirstName:“&locationidValue&”LastName:“_
&状态值)
控制台写入(vbCrLf)
下一个
捕获错误变量作为异常
“错误捕获
Console.Write(errorVariable.ToString())
结束尝试
"清理溪流,。
reader.Close()
dataStream.Close()
答复:Close()
端接头
末级
端模块

一个选项是使用。其中一个重载是流,我怀疑您正在从呼叫中恢复。e、 g

XDocument.Load(yourStream)

…只要您有有效的XML。

与George的答案类似,但这次使用类与使用LINQ技术/功能的XDocument类相比,您可能不需要:

Dim x As New XmlDocument()
x.Load("http://localhost/TheXMLFileToLoad.xml")
但在您的例子中,在POST操作之后,您可能已经在内存中有了XML流或字符串。在这种情况下,您可以执行以下操作之一来加载后置流结果:

x.Load(yourStream)
或者如果您的帖子结果为
字符串

x.LoadXml(postXMLString)

那么,我是将URL放在括号中,还是将表示从URL获取的数据的值放在括号中?还是没关系?@Gilles:
XDocument.Parse将允许您将实际的XML内容输入其中。@neolik——当然,这就是我指出重载是流的原因。通常,当你抓取一个web响应时,它会以流的形式返回。@George:我的印象是OP不喜欢流,因此评论如下:而且OP似乎在接受后忘记了+1你的答案,所以这里有一个解决方案!谢谢大家,我稍后会尝试发布一些代码,这是相当长的一段时间啊…好的,XML流is@Gilles:如果出现任何相关信息,请始终更新原始问题。特别是对于源代码片段,因为它们没有格式化/突出显示以获得更好的可见性。注意我刚才是如何更新你的问题的。好的,谢谢你,好的,先生好的,无论出于什么原因,我试着这么做,这就是我的语法,但无论出于什么原因,它都不会例外:-/当你尝试加载XML时,有什么症状/错误?查看您的代码示例,我不确定您为什么要使用这行代码返回服务器
XDocument.Load(“https://quickvin