0

I am attempting to read an XML response from a ASP.NET Web API into a DataSet so that I can bind it to a DropDownList. When reading the response however, I am met with Data at the root level is invalid. Line 1, position 1.

Snipp:

WebRequest request = WebRequest.Create("EndPointURL");
request.Method = "GET";
WebResponse response = request.GetResponse();

DataSet ds = new DataSet();

using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
    ds.ReadXml(rdr); //EXCEPTION Data at the root level is invalid. Line 1, position 1.
}

Sample of XML response:

<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MySchema">
    <Person>
        <Address1 xmlns="http://schemas.datacontract.org/2004/07/ClassSchema">123 Main Street</Address1>
        <Address2 xmlns="http://schemas.datacontract.org/2004/07/ClassSchema"">None</Address2>
        ...FOR BREVITY
    </Person>
    <Person>
        ..FOR BREVITY
    </Person>
    .
    .
    .
</ArrayOfPerson>

I have successfully made this call to other end points of the API so I'm a little stumped. I do not see anything at Line 1, position 1 that looks like an invalid XML character. What could possibly be throwing the error?

KidBatman
  • 585
  • 1
  • 13
  • 27
  • 1
    Sounds like there might be a byte order mark at the beginning of the stream. – dbc Apr 01 '15 at 20:02
  • Doesnt that stream need decoded? What character encoding is used? For debugging can you just load it into a string an inspect it? – Stewart_R Apr 01 '15 at 20:05
  • @dbc How do I verify that a byte order mark is indeed the issue? And if it is, would I need to resolve it at the API level or in my client code? – KidBatman Apr 01 '15 at 20:53
  • Possibly related: https://stackoverflow.com/questions/26774752/xdocument-loadfeedurl-returns-data-at-the-root-level-is-invalid-line-1-posi – dbc Apr 01 '15 at 22:10

2 Answers2

0

Try :

ReadXml(XmlReader)

Like so :

XmlTextReader reader = new XmlTextReader(rdr);
ds.ReadXml(reader);
C1rdec
  • 1,647
  • 1
  • 21
  • 46
0

Sounds like you may be using the wrong encoding. You could try setting the encoding from the HttpWebResponse:

        var ds = new DataSet();

        WebRequest request = WebRequest.Create(requestUriString);
        request.Method = "GET";
        using (var response = (System.Net.HttpWebResponse)request.GetResponse())
        {
            // get correct charset and encoding from the server's header
            Encoding encoding;
            try
            {
                encoding = Encoding.GetEncoding(response.CharacterSet);
            }
            catch
            {
                encoding = Encoding.UTF8;
            }

            using (var rdr = new StreamReader(response.GetResponseStream(), encoding))
            {
                ds.ReadXml(rdr);
            }
        }

Note this is a simplified version of complex answers here: Encoding problem with HttpWebResponse. Those answers suggest picking the actual encoding out of the returned HTML and reloading the stream if needed. You could try to do something similar by picking the encoding out of the XML declaration, if you really need to do so.

dbc
  • 104,963
  • 20
  • 228
  • 340