为什么包含 XML 标头时 C# XmlDocument.LoadXml(string) 会失败? [英] Why does C# XmlDocument.LoadXml(string) fail when an XML header is included?

查看:48
本文介绍了为什么包含 XML 标头时 C# XmlDocument.LoadXml(string) 会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么下面的代码示例失败并显示 XmlException根级别的数据无效.第 1 行,位置 1."

var body = " ......"XmlDocument bodyDoc = new XmlDocument();bodyDoc.LoadXml(body);

解决方案

背景

虽然您的问题确实将编码设置为 UTF-16,但您没有正确转义字符串,所以我不确定您是否确实将字符串准确地转置到您的问题中.

我遇到了同样的异常:

<块引用>

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

但是,我的代码如下所示:

string xml = "This is a Test";XmlDocument xmlDoc = new XmlDocument();xmlDoc.LoadXml(xml);

问题

问题是字符串在 .NET 中内部存储为 UTF-16,但是 XML 文档标头中指定的编码可能不同.例如:

来自字符串的 MSDN 文档此处:><块引用>

字符串中的每个 Unicode 字符是由 Unicode 标量值定义,也称为 Unicode 代码点或的序数(数字)值Unicode 字符.每个码点是使用 UTF-16 编码进行编码,并且每个元素的数值编码由一个字符表示对象.

这意味着当您通过 XmlDocument.LoadXml() 传递带有 XML 标头的字符串时,它必须说明编码为 UTF-16.否则,实际的底层编码将与标头中报告的编码不匹配,并导致抛出 XmlException.

解决方案

此问题的解决方案是确保在您传递 Load 或 LoadXml 方法的任何内容中使用的编码与您在 XML 标头中所说的内容相匹配.在我上面的示例中,将您的 XML 标头更改为状态 UTF-16 或将输入编码为 UTF-8 并使用 XmlDocument.Load 方法.

以下示例代码演示了如何使用 MemoryStream 使用定义 UTF-8 编码 XML 文档的字符串构建 XmlDocument(当然,存储的是 UTF-16 .NET 字符串).

string xml = "This is a Test";//将 XML 字符串编码为 UTF-8 字节数组字节 [] 编码字符串 = Encoding.UTF8.GetBytes(xml);//将字节数组放入流中并倒回开头MemoryStream ms = new MemoryStream(encodedString);ms.flush();ms.Position = 0;//从 UTF-8 编码字节的 MemorySteam 构建 XmlDocumentXmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(ms);

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" ?>
<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" ?>
<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);

这篇关于为什么包含 XML 标头时 C# XmlDocument.LoadXml(string) 会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆