JL.
JL.

Reputation: 81262

"Data at the Root Level is invalid" with LoadXml

I have a code snippet :

XmlDocument doc = new XmlDocument();
try
{
    doc.LoadXml(xmlPath);
}
catch (Exception ex)
{
    string exMessage = ex.Message; 
}

The XML looks like this

  <?xml version="1.0" encoding="UTF-8"?>
  <MimeTypes>
   <MimeType>
     <Extension>.3dm</Extension>
     <Value>x-world/x-3dmf</Value>
   </MimeType>
  </MimeTypes>

Its producing this error:

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

Any idea what's wrong?

Upvotes: 1

Views: 2702

Answers (3)

unknown
unknown

Reputation:

does xmlPath contains the whole xml or a path to a file that contains it? The LoadXml method expects the actual XML, not a path to a file. If you want to load the xml using a path, using the Load method.

Upvotes: 1

Daniel Earwicker
Daniel Earwicker

Reputation: 116654

You're passing a file path to a parameter that should contain the XML itself.

Upvotes: 3

ristonj
ristonj

Reputation: 1608

Use doc.Load(xmlPath). LoadXML is for loading an XML string.

Upvotes: 9

Related Questions