1

I am currently facing an issue when i get response from webservice, after reading response in response stream i want to load this response stream using xmltextreader. but when Load(xmlTextreaderObj) is called, it raises an exception displaying that "Root Element is Missing". Here is code which am using. Please help

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
4
  • what does your XML looking like? Commented May 5, 2011 at 11:34
  • i am returning a string from Webservice Method, this string is as follows ServiceResponse = "<?xml version=\"1.0\"?><RatesResponse><Rates>" + currRate .ToString() + "</Rates></RatesResponse>"; Commented May 5, 2011 at 11:52
  • i am using following url to call asmx webservice, there might be issue... here is url localhost url as "localhost/AzkarRatesService/…" + originstate + "&destState=" + deststate + "&Weight=" + weight Commented May 5, 2011 at 12:07
  • You should go back to XmlReader.Create. Also, get rid of the ReadToEnd. Commented May 5, 2011 at 16:28

1 Answer 1

1

If your real code looks as its written here, you'll probably getting the errror because you've already read the stream though once, on the first row.

Dim Resp As String = respStream.ReadToEnd()

The stream is one way only, so when you create a xmlreader it will start reading from where the stream is at, and it will no longer point to the root element (but at the end of the stream)

Sign up to request clarification or add additional context in comments.

3 Comments

ok but when i am not using this line of code, the problem is still there... sometime it popsup for root element missing and sometime it says that starting tag of td is missing its ending tag of td... I don't know whats happening here.... could you tell me that way of passing parametrs to asmx service which i am using is ok? Please help.... Thanks
i suspect there is nothing wrong with the parameter passing, the error you're getting is from the actual xml parsing. if you read everything into a string (like you do on line 1 in your example) does the xml look alright? if so you're probably doing a read from the stream somehow before passing it to XmlReader. Try loading the reader directly into the xmlDocument, without first reading the attribute straight from the reader.
ah, i see in your other comment that you're creating the xml as a string. beware that .net strings use unicode encoding, this can mess up readers that arent expecting that, also, if you mix encodings you'll also run into problems. if possible try generating the xml with XmlWriter or XElement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.