当XML头包括为什么C#XmlDocument.LoadXml(串)会失败?XML、XmlDocument、LoadXml

2023-09-02 10:19:49 作者:奶气丫头自然萌

有没有人有任何想法,为什么下面的code样品失败,出现XmlException数据在根级别无效。行1,位置1。

  VAR身体=< XML版本=1.0编码=UTF-16><报告> ......
XmlDocument的bodyDoc =新的XmlDocument();
bodyDoc.LoadXml(体);
 

解决方案

背景

虽然你的问题确实有编码设置为UTF-16,你没有字符串正确转义,所以我不知道,如果你没有,其实,准确地转串入你的问题。

我遇到了同样的异常:

  

System.Xml.XmlException:数据在   根级别无效。 1号线,   位置1。

不过,我的code是这样的:

  XML字符串=< XML版本= 1.0 编码= UTF-8 >吗? n<事件>这是一个测试< /事件>中;
的XmlDocument xmlDoc中=新的XmlDocument();
xmlDoc.LoadXml(XML);
 

问题

的问题是,串存储在内部为UTF-16在.NET然而在XML文档标题中指定的编码可以是不同的。例如:

 < XML版本=1.0编码=UTF-8&GT?;
 
xml语言,dtd约束是什么,xml的属性语法,xml文档的dom树的讲解

从MSDN文档字符串这里:

  

在一个字符串中的每个统一code字符   通过一个统一code标值来定义,   也被称为统一code code点或   的序号(数字)值   统一code字符。每个code点   EN codeD使用UTF-16编码,以及   的各元件的数值   编码是由一个字符psented重新$ P $   对象。

这意味着,当你通过XmlDocument.LoadXml()的字符串XML头,它必须说,编码是UTF-16。否则,实际的底层编码将不匹配报头中编码,并会导致一个XmlException被抛出。

解决方案

有关此问题的解决方案是确保用于无论你通过加载或者你说这是在XML头中的loadXML方法相匹配的编码。在我上面的例子中,要么改变你的XML头状态的UTF-16或EN code的输入UTF-8并使用的 XMLDocument.load方法方法的。

下面是示例code演示如何使用一个MemoryStream使用它定义一个UTF-8 EN code XML文档的字符串来构建一个XmlDocument(当然,存储在UTF-16 .NE​​T字符串)。

  XML字符串=< XML版本= 1.0 编码= UTF-8 >吗? n<事件>这是一个测试< /事件>中;

// EN code在UTF-8字节数组的XML字符串
byte []的EN codedString = Encoding.UTF8.GetBytes(XML);

//把字节数组到一个流,它绕回到开头
MemoryStream的毫秒=新的MemoryStream(EN codedString);
ms.Flush();
ms.Position = 0;

//生成的XmlDocument从UTF-8的MemorySteam EN codeD字节
的XmlDocument xmlDoc中=新的XmlDocument();
xmlDoc.Load(毫秒);
 

Does anyone have any idea why the following code sample fails with an XmlException "Data at the root level is invalid. Line 1, position 1."

var body = "<?xml version="1.0" encoding="utf-16"?><Report> ......"
XmlDocument bodyDoc = new XmlDocument();            
bodyDoc.LoadXml(body);

解决方案

Background

Although your question does have the encoding set as UTF-16, you don't have the string properly escaped so I wasn't sure if you did, in fact, accurately transpose the string into your question.

I ran into the same exception:

System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

However, my code looked like this:

string xml = "<?xml version="1.0" encoding="utf-8" ?>n<event>This is a Test</event>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

The Problem

The problem is that strings are stored internally as UTF-16 in .NET however the encoding specified in the XML document header may be different. E.g.:

<?xml version="1.0" encoding="utf-8"?>

From the MSDN documentation for String here:

Each Unicode character in a string is defined by a Unicode scalar value, also called a Unicode code point or the ordinal (numeric) value of the Unicode character. Each code point is encoded using UTF-16 encoding, and the numeric value of each element of the encoding is represented by a Char object.

This means that when you pass XmlDocument.LoadXml() your string with an XML header, it must say the encoding is UTF-16. Otherwise, the actual underlying encoding won't match the encoding reported in the header and will result in an XmlException being thrown.

The Solution

The solution for this problem is to make sure the encoding used in whatever you pass the Load or LoadXml method matches what you say it is in the XML header. In my example above, either change your XML header to state UTF-16 or to encode the input in UTF-8 and use one of the XmlDocument.Load methods.

Below is sample code demonstrating how to use a MemoryStream to build an XmlDocument using a string which defines a UTF-8 encode XML document (but of course, is stored a UTF-16 .NET string).

string xml = "<?xml version="1.0" encoding="utf-8" ?>n<event>This is a Test</event>";

// Encode the XML string in a UTF-8 byte array
byte[] encodedString = Encoding.UTF8.GetBytes(xml);

// Put the byte array into a stream and rewind it to the beginning
MemoryStream ms = new MemoryStream(encodedString);
ms.Flush();
ms.Position = 0;

// Build the XmlDocument from the MemorySteam of UTF-8 encoded bytes
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);

 
精彩推荐
图片推荐