by Dominic Zukiewicz
27. April 2007 15:28
Considering how much XML is used and the amount of code I've written to read/write XML, I still make a common mistake!
If you ever get this error:
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
Make sure you are using the correct method for the XmlDocument class. For example:
string xml = @"<xml version=""1.0""?><node><innerNode/></node>";
string filename = @"C:\\temp.xml";
StreamWriter sw = new StreamWriter(filename);
sw.Write(xml);
sw.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(filename); // <-- Error right here!
This overload is for loading TEXT and not files.
So remember
- Load () for files and stream
- LoadXml() to load XML strings!
Easily mis-understood, so learn it or spend several hours debugging!!

e1d477b3-5f89-48c8-add1-fde1155b8cf0|0|.0
Tags:
Framework | Xml