3

我收到一个 xml 响应,现在我想解析它。

目前我必须收到的 XML 响应是:

    Dim textReader = New IO.StreamReader(Request.InputStream)

    Request.InputStream.Seek(0, IO.SeekOrigin.Begin)
    textReader.DiscardBufferedData()

    Dim Xmlin = XDocument.Load(textReader)

我现在如何继续处理并挑选元素值?

<subscription>
<reference>abc123</reference>
<status>active</status>
<customer>
    <fname>Joe</fname>
    <lname>bloggs</lname>
    <company>Bloggs inc</company>
    <phone>1234567890</phone>
    <email>joebloggs@hotmail.com</email>
 </customer>
 </subscription>

如果我有字符串格式,我可以使用

    Dim xmlE As XElement = XElement.Parse(strXML) ' strXML is string version of XML

    Dim strRef As String = xmlE.Element("reference")

我需要将 request.inputstream 转换为 strign 格式还是有其他更好的方法?

谢谢

4

2 回答 2

1

我需要将 request.inputstream 转换为 strign 格式还是有其他更好的方法?

您可以直接从请求流中加载它,无需将其转换为字符串:

Request.InputStream.Position = 0
Dim Xmlin = XDocument.Load(Request.InputStream)
Dim reference = Xmlin.Element("subscription").Element("reference").Value

或者:

Dim reference = Xmlin.Descendants("reference").First().Value
于 2012-06-19T12:01:01.407 回答
1

最后经过大量测试,我只能让它工作:

    Dim textReader = New IO.StreamReader(Request.InputStream)

    Request.InputStream.Seek(0, IO.SeekOrigin.Begin)
    textReader.DiscardBufferedData()

    Dim Xmlin = XDocument.Load(textReader)

    Dim strXml As String = Xmlin.ToString

    Dim xmlE As XElement = XElement.Parse(strXml)
    Dim strRef As String = xmlE.Element("reference")
    Dim strStatus As String = xmlE.Element("status")
    Dim strFname As String = xmlE.Element("customer").Element("fname").Value()
    Dim strLname As String = xmlE.Element("customer").Element("lname").Value()
    Dim strCompany As String = xmlE.Element("customer").Element("company").Value()
    Dim strPhone As String = xmlE.Element("customer").Element("phone").Value()
    Dim strEmail As String = xmlE.Element("customer").Element("email").Value()
于 2012-06-24T13:50:22.070 回答