Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Loading Dataset from xml string: "xml data at root is invalid"

Status
Not open for further replies.

nimarii

MIS
Jan 26, 2004
213
US
Hello,

I created a user control in c#.net to display google maps. In the event that a location is missing a latitude and longitude, I make a call to the google service, which returns xml. I want to load a dataset with this xml data, but I get an error: "Data at root is invalid. Line 1 position 1."

This is the code that I'm using. The error occurs on the second to last line (DS.ReadXml(tx);)
Code:
URL = "[URL unfurl="true"]http://maps.google.com/maps/geo?q="[/URL] + GP.Address + 
"&output=xml&key=" + GoogleAPIKey;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
string result = "";
Stream response = webresponse.GetResponseStream();
System.IO.StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));
result = readStream.ReadToEnd();
StringReader tx = new StringReader(result);
DataSet DS = new DataSet();
//string filename = "testload2.xml";
DS.ReadXml(tx);
readStream.Close();

This is the xml that is returned from the URL

<?xml version="1.0" encoding="UTF-8" ?><kml xmlns="/kml/2.0"><Response><name>9140 Woodend Road, Edwardsville, KS, 66111</name>
<Status><code>200</code><request>geocode</request></Status><Placemark
id="p1"><address>9140 Woodend Rd, Kansas City, KS 66111, USA</address>
<AddressDetails Accuracy="8"
xmlns="urn:eek:asis:names:tc:ciq:xsdschema:xAL:2.0"><Country>
<CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName>
<AdministrativeArea><AdministrativeAreaName>KS</AdministrativeAreaName>
<Locality><LocalityName>Kansas City</LocalityName><Thoroughfare>
<ThoroughfareName>9140 Woodend Rd</ThoroughfareName></Thoroughfare>
<PostalCode><PostalCodeNumber>66111</PostalCodeNumber></PostalCode>
</Locality></AdministrativeArea></Country></AddressDetails><ExtendedData>
<LatLonBox north="39.0542214" south="39.0479262" east="-94.7899011"
west="-94.7961963" /></ExtendedData><Point>
<coordinates>-94.7930487,39.0510738,0</coordinates></Point></Placemark>
</Response></kml>

The xml is well-formed, and if I save it to a file and then load the file, it works. But I need it to be able to read it from the stream, or from the string.

People have mentioned that deleting the vti_cnf folder will fix the problem, but I've already tried that, and it didn't do anything.

I've run out of things on the internet to read. Does anyone have some insight into this, or have an idea for a different solution?

Thanks!
nimarii
 
xml != dataset
You need to parse the values from the xml and load a dataset with it.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
You'll have to save the stream to a file then read it into the dataset
 
The op's logic is fine. The issue is most definitely related to the GetEncoding() part. Try leave it out or change it to utf-16, see if it cures the problem. It is the encoding which causes the problem.
 
Upon properly looking at it from the narrow-down post rather than the original wide-post, I would have proposed this instead. You should directly feed the StreamReader to ReadXml method. Try this.
[tt]
URL = "[ignore][/ignore]" + GP.Address +
"&output=xml&key=" + GoogleAPIKey;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
[red]//[/red]string result = "";
Stream response = webresponse.GetResponseStream();
System.IO.StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));
[red]//[/red]result = readStream.ReadToEnd();
[red]//[/red]StringReader tx = new StringReader(result);
DataSet DS = new DataSet();
//string filename = "testload2.xml";
[red]//[/red]DS.ReadXml(tx);
DS.ReadXml([red]readStream[/red]);
readStream.Close();
[/tt]
 
hi guys!
well the site wasn't letting me log in, so i had to create a new username/handle to get in here.

Turns out that it was encoding related after all. There was a BOM(Byte Order Mark) preceeding the data that is used to specify the encoding type. Apparently, when you view it in notepad, it won't show the BOM characters.
The link has more details:

I just changed one line of code from to make the StreamReader check to see if there's a BOM:
Code:
System.IO.StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));

to:
Code:
System.IO.StreamReader readStream = new StreamReader(response, true);

Thanks so much to all for responding! Special thanks to tsuji!!
 
well turns out that my error came back. solution was to specify the encoding in the http call: &oe=utf-8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top