使用xmltextreader读取webresponse然后xmlDocument.load(xmlTextReaderobj)时出现XML异常

时间:2011-05-05 11:28:58

标签: xml vb.net exception

当我从webservice获得响应时,我正面临一个问题,在响应流中读取响应后,我想使用xmltextreader加载此响应流。但是当调用Load(xmlTextreaderObj)时,它会引发一个异常,显示“Root Element is Missing”。这是我正在使用的代码。请帮忙

Dim wResp As HttpWebResponse = CType(wReq.GetResponse(), HttpWebResponse)
Dim respStream As StreamReader = New StreamReader(wResp.GetResponseStream())

Dim Resp As String = respStream.ReadToEnd()

Dim xmlReader As XmlTextReader = New XmlTextReader(respStream)
''Dim Reader As XmlReader = XmlReader.Create(wResp.GetResponseStream())

If xmlReader.AttributeCount >= 0 Then
    Try
        Dim xmlRateQuote As XmlDocument = New XmlDocument()

        xmlRateQuote.Load(xmlReader)


        'The entire XML Response String
        Dim response As String = xmlRateQuote.InnerXml

        wReq.Abort()
        wResp.Close()
    Catch ex As Exception


    End Try
End If

1 个答案:

答案 0 :(得分:0)

如果您的真实代码看起来像在这里写的那样,您可能会得到错误,因为您已经在第一行读过了一次流。

Dim Resp As String = respStream.ReadToEnd()

流只是一种方式,所以当你创建一个xmlreader时,它将从流所在的位置开始读取,它将不再指向根元素(但在流的末尾)