本文介绍了未定义实体 &nbsp XML 解析异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 <!doctype/> 声明中定义 non-breakspace entity.代码:

I am trying to define non breaking space entity in the <!doctype /> declaration. Code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [ <!ENTITY nbsp "&#x00A0;"> ] />

 <html xmlns="http://www.w3.org/1999/xhtml">
  .. .... 

但是当我从上面的文档创建一个 XDocument 对象时,我得到一个异常

But still when i am creating an XDocument object from the above document, i am getting an exception

Undefined entity &nbsp. 

我做错了什么?

推荐答案

首先,中外部和内部实体的结合"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [ <!ENTITY nbsp "&#x00A0;">] 是不必要的,因为 http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd 包括 www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent 其中包括 <!ENTITY nbsp "&#160;"> 与您的完全相同.

Firstly, the combination of external and internal entities in "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [ <!ENTITY nbsp "&#x00A0;"> ] is unnecessary because http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd includes www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent which includes <!ENTITY nbsp "&#160;"> which is exactly the same as yours.

所以那一点应该是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

其次,XDocument 默认不验证 DTD.但是,您可以使用适当的设置从 XmlValidatingReaderXmlTextReaderLoad

Secondly, XDocument doesn't validate to a DTD by default. However, you can Load from an XmlValidatingReader or XmlTextReader with the appropriate settings

[如果不可能,让它成为可能!]最好将 XmlReader 设置为使用 XmlPreloadedResolver,因为有很多人在那个 DTD 的 URI 和它包含的 .ent 文件上击中可怜的 W3C,他们完全可以使用它的本地副本(完全可以"我的意思是,如果每个人都愿意,他们会非常高兴停止敲打他们的服务器).除了对他们有好处,对你来说也会更快.

[If it's not possible, make it possible!] it's a good idea to set the XmlReader to use a XmlPreloadedResolver, because there's a lot of people hitting poor ol' W3C at the URI of that DTD and the .ent files it includes, and they're totally okay with you using a local copy of it (by "totally okay" I mean they'd be really glad if everyone would stop hammering their server quite so much). As well as being good for them, it'll be much faster for you.

这篇关于未定义实体 &amp;nbsp XML 解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:54