C# 使用 XML 时引用未声明的实体异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/281682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:28:38  来源:igfitidea点击:

Reference to undeclared entity exception while working with XML

c#xmldtd

提问by Rob

I am trying to set the innerxml of a xmldoc but get the exception: Reference to undeclared entity

我正在尝试设置 xmldoc 的 innerxml 但得到异常:引用未声明的实体

XmlDocument xmldoc = new XmlDocument();
string text = "Hello, I am text α   – —"
xmldoc.InnerXml = "<p>" + text + "</p>";

This throws the exception:

这会引发异常:

Reference to undeclared entity 'alpha'. Line 2, position 2..

对未声明实体“alpha”的引用。第 2 行,位置 2..

How would I go about solving this problem?

我将如何解决这个问题?

采纳答案by Stephan Leclercq

XML, unlike HTML does not define entities (ie named references to UNICODE characters) so &alpha; &mdash; etc. are not translated to their corresponding character. You must use the numerical value instead. You can only use &lt; and &amp; in XML

与 HTML 不同,XML 不定义实体(即对 UNICODE 字符的命名引用),因此 α — 等没有被翻译成它们对应的字符。您必须改用数值。您只能使用 < 和 & 以 XML 格式

If you want to create HTML, use an HtmlDocument instead.

如果要创建 HTML,请改用 HtmlDocument。

回答by FlySwat

Try replacing &Alpha with

尝试用 &Alpha 替换

  &#913;

回答by Fernando Miguélez

The preceding answer is right. Another alternative is to link your html document to the DTD where those character entities are defined, and that is standard XHTML DTD definition. Your xml file should include the following declaration:

前面的回答是对的。另一种选择是将您的 html 文档链接到定义这些字符实体的 DTD,这是标准的 XHTML DTD 定义。您的 xml 文件应包含以下声明:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">

回答by configurator

You could also set the InnerText to "Hello, I am text α – —", making the XmlDocument escape them automatically. I think.

您还可以将 InnerText 设置为"Hello, I am text α – —",使 XmlDocument 自动转义它们。我认为。

回答by LandedGently

In .Net, you can use the System.Xml.XmlConvertclass:

在 .Net 中,您可以使用System.Xml.XmlConvert该类:

string text = XmlConvert.EncodeName("Hello &alpha;");

Alternatively, you can declare the entities locally by putting the declarations between square brackets in a DOCTYPE declaration. Add the following header to your xml:

或者,您可以通过将声明放在 DOCTYPE 声明中的方括号之间来在本地声明实体。将以下标题添加到您的 xml 中:

<!DOCTYPE documentElement[
<!ENTITY Alpha "&#913;">
<!ENTITY ndash "&#8211;">
<!ENTITY mdash "&#8212;">
]>

Do a google on "html character entities" for the entity definitions.

对实体定义的“html 字符实体”进行谷歌搜索。

回答by Nick Josevski

The use of a HtmlDocument wasn't suitable in my situation, our system had a custom XmlUrlResolver which we made use of for loading the xml.

HtmlDocument 的使用不适合我的情况,我们的系统有一个自定义的 XmlUrlResolver,我们用它来加载 xml。

//setup
public class CustomXmlResolver : XmlUrlResolver { /* ... */ }
String originalXml; //fetched xml with html entities in it

var doc = new XmlDocument();
doc.XmlResolver = new AdCastXmlResolver();

//making use of a transitional dtd
doc.LoadXml("<!DOCTYPE html SYSTEM \"xhtml1-transitional.dtd\" > " + originalXml);

回答by verbedr

Use string System.Net.WebUtility.HtmlDecode(string) which will decode all HTML entity encoded characters to its Unicode variant. It is available from dot.net framework 4

使用字符串 System.Net.WebUtility.HtmlDecode(string) 将所有 HTML 实体编码字符解码为其 Unicode 变体。它可从 dot.net framework 4 获得

回答by dret

If you do want to use the HTML entity names you are used to, the W3C has got you covered and has produced "XML Entity Definitions for Characters" http://www.w3.org/TR/xml-entity-names/, which essentially is a list of named entities very similar to the ones that HTML has. But as mentioned above, this is not built into XML, and needs to be explicitly supported by XML applications that want to use these named entities.

如果您确实想使用您习惯的 HTML 实体名称,W3C 已经为您提供了帮助,并制作了“字符的 XML 实体定义” http://www.w3.org/TR/xml-entity-names/,它本质上是一个命名实体的列表,与 HTML 的命名实体非常相似。但是如上所述,这不是内置于 XML 中的,需要希望使用这些命名实体的 XML 应用程序明确支持。