2012-06-18 47 views
2

我收到一個xml響應,我現在想解析這個。xml response http post - 將request.inputstream轉換爲字符串 - asp.net

目前我有什麼接收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>[email protected]</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格式或有另一種更好的辦法?

感謝

+0

使用XmlDocument。有了它,你可以調用它的[加載](http://msdn.microsoft.com/en-us/library/875kz807)方法的URL,它會把它一切都很好。 [閱讀全文](http://msdn.microsoft.com/en-us/library/6kza7w4k) – Jeremy

+0

不僅XmlDocument,但一旦你得到它,你可以使用LINQ來獲取數據,甚至可能更快。 – jcolebrand

+0

但是,我不得不問,爲什麼你會這樣收到它,因爲如果你喜歡,我們當然可以將它作爲鍵入的XML接收。 – jcolebrand

回答

1

中底:

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

或我只能得到這個工作:

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() 
1

我需要的request.inputstream轉換爲strign格式或有另一種更好的辦法?

你可以直接從請求流加載它,您無需將其轉換爲字符串:多的測試後

Dim reference = Xmlin.Descendants("reference").First().Value 
+0

是的,這是我原來的解決方案點鏈接由OP提供! –

+0

具體而言,我想使用VB.NET的軸查詢:'xmlIn。'。 –