使用LoadXml“根级的数据无效”

时间:2021-10-15 18:33:37

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是这样的。

  <?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.

根级的数据无效。1号线,位置1。

Any idea what's wrong?

知道有什么问题吗?

3 个解决方案

#1


10  

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

使用doc.Load(xmlPath)。LoadXML用于加载XML字符串。

#2


3  

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

将文件路径传递给应该包含XML本身的参数。

#3


1  

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.

xmlPath是否包含整个xml或包含它的文件的路径?LoadXml方法需要实际的XML,而不是文件的路径。如果希望使用路径加载xml,请使用load方法。

#1


10  

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

使用doc.Load(xmlPath)。LoadXML用于加载XML字符串。

#2


3  

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

将文件路径传递给应该包含XML本身的参数。

#3


1  

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.

xmlPath是否包含整个xml或包含它的文件的路径?LoadXml方法需要实际的XML,而不是文件的路径。如果希望使用路径加载xml,请使用load方法。