tags:

views:

100

answers:

2

I am trying to load a very basic XML document but everytime I get to the LoadXml(string url) line, the program crashes and reports an exception ("Data at the root level is invalid. Line 1, position 1" XmlException).

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(@"C:\Websites\TestHarness\TestHarness\TestHarness\ExampleXml.xml");     
XmlNode node = xmldoc.DocumentElement;

My XML looks like this (this is a sample xml document from W3Schools and it opens in IE fine):

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

This is pasted exactly as is with no whitespace.

I can't see anything wrong with this code, the stack trace doesn't tell me much and I suspect there is an environmental issue somewhere. Does anyone have any ideas?

EDIT: The formatting of the XML isn't right. The XML is the same as the sample document on here: http://w3schools.com/xml/default.asp

+5  A: 

Use Load() instead of LoadXml().

Anton Gogolev
+1 d'oh... 46 seconds too fast for me ;-p
Marc Gravell
+1  A: 

Yes, you are loading the file name as xml. But also you are missing the xml encoding.... Xml format don't allow any text just like that. That xml should written live this:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don&#39;t forget me this weekend!</body>
</note>

Here is a nice tool to encode online.

Hope this helps... :)

Lucas