xml doc.loadxml not reading

T

tshad

I have a small xml document:

<book>
<title>The Title</title>
</book>

I am trying to read this document:

XmlDocument doc = new XmlDocument();
doc.LoadXml(filePath);

But on the doc.load, I get:

Data at the root level is invalid. Line 1, position 1.

Why is this?

IE shows the file fine.

What is wrong with the root?

Thanks,

Tom
 
P

Pavel Minaev

I have a small xml document:

<book>
   <title>The Title</title>
</book>

I am trying to read this document:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(filePath);

But on the doc.load, I get:

Data at the root level is invalid. Line 1, position 1.

Why is this?

Because XmlDocument.LoadXml() is used to load XML from the _string_
you pass it as an argument, not from a file named by the argument. In
other words, it tried to parse the contents of filePath as XML (which
it obviously wasn't).

Just use XmlDocument.Load(). Or, better yet, use
System.Xml.Linq.XDocument - unless you specifically need DOM classes
(because e.g. some API expects them), XLINQ API is much cleaner and
easier to use.
 
P

Pavel Minaev

I have a small xml document:

<book>
   <title>The Title</title>
</book>

I am trying to read this document:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(filePath);

But on the doc.load, I get:

Data at the root level is invalid. Line 1, position 1.

Why is this?

Because XmlDocument.LoadXml() is used to load XML from the _string_
you pass it as an argument, not from a file named by the argument. In
other words, it tried to parse the contents of filePath as XML (which
it obviously wasn't).

Just use XmlDocument.Load(). Or, better yet, use
System.Xml.Linq.XDocument - unless you specifically need DOM classes
(because e.g. some API expects them), XLINQ API is much cleaner and
easier to use.
 
T

tshad

I have a small xml document:

<book>
<title>The Title</title>
</book>

I am trying to read this document:

XmlDocument doc = new XmlDocument();
doc.LoadXml(filePath);

But on the doc.load, I get:

Data at the root level is invalid. Line 1, position 1.

Why is this?

Because XmlDocument.LoadXml() is used to load XML from the _string_
you pass it as an argument, not from a file named by the argument. In
other words, it tried to parse the contents of filePath as XML (which
it obviously wasn't).

Just use XmlDocument.Load(). Or, better yet, use
System.Xml.Linq.XDocument - unless you specifically need DOM classes
(because e.g. some API expects them), XLINQ API is much cleaner and
easier to use.

That worked.

Thanks,

Tom
 
T

tshad

I have a small xml document:

<book>
<title>The Title</title>
</book>

I am trying to read this document:

XmlDocument doc = new XmlDocument();
doc.LoadXml(filePath);

But on the doc.load, I get:

Data at the root level is invalid. Line 1, position 1.

Why is this?

Because XmlDocument.LoadXml() is used to load XML from the _string_
you pass it as an argument, not from a file named by the argument. In
other words, it tried to parse the contents of filePath as XML (which
it obviously wasn't).

Just use XmlDocument.Load(). Or, better yet, use
System.Xml.Linq.XDocument - unless you specifically need DOM classes
(because e.g. some API expects them), XLINQ API is much cleaner and
easier to use.

That worked.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top