1

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

2 Answers 2

5

Use Load() instead of LoadXml().

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

Comments

1

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... :)

Comments

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.