在 c# 中从 URL 读取 xml 时引用未声明的实体 'nbsp'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15701745/
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
Reference to undeclared entity 'nbsp' while reading xml from URL in c#?
提问by Mahipat
XmlDocument xmldoc = new XmlDocument();
xmldoc.XmlResolver = null;
xmldoc.Load("URL");
XmlWriter xmlWrite = XmlWriter.Create(@Server.MapPath("Test.xml"));
xmldoc.Save(xmlWrite);
xmlWrite.Close();
Above is the code I am using to read an XML file. The XML I am loading contains entities like nbsp;
and because of that, the code is throwing an XMLException:
以上是我用来读取 XML 文件的代码。我正在加载的 XML 包含类似的实体nbsp;
,因此,代码抛出一个 XMLException:
Reference to undeclared entity 'nbsp'
对未声明实体 'nbsp' 的引用
采纳答案by Raman
There are two ways to handle this, but for both you need to make changes to the input file.
有两种方法可以处理此问题,但对于这两种方法,您都需要对输入文件进行更改。
1) Change your
to  
in you input files. It is always better to use Unicode and avoid readable entities in XML documents. Use plain Unicode characters or their numerical form.
1)
将 
您的输入文件更改为。最好使用 Unicode 并避免 XML 文档中的可读实体。使用纯 Unicode 字符或其数字形式。
2) If you still need/want to use
You can declare a custom doctype in the file that will do the conversion for you:
2)如果您仍然需要/想要使用
您可以在文件中声明一个自定义文档类型来为您进行转换:
<!DOCTYPE doctypeName [
<!ENTITY nbsp " ">
]>
This problem is coming because
isn't one of XMLs predefined entities.
这个问题即将到来,因为
它不是 XML 预定义实体之一。
回答by Kevin Lee Garner
I had this same problem today, and I was able to solve it by changing the HTML entity name to an HTML entity number.
我今天遇到了同样的问题,我能够通过将 HTML 实体名称更改为 HTML 实体编号来解决它。
Per this helpful forum post, I was able to fix this error.
根据这个有用的论坛帖子,我能够修复这个错误。
HTML entity names are not well supported in XML. This is why the "nonbreaking space"--"nbsp"--triggers this frustrating error.
HTML 实体名称在 XML 中没有得到很好的支持。这就是“不间断空间”——“nbsp”——触发这个令人沮丧的错误的原因。
The easiest way to fix this issue is to use HTML entity numbers, which means changing
to  
.
解决此问题的最简单方法是使用 HTML 实体编号,这意味着更改
为 
.
For a comprehensive listing of HTML entities with names and numbers, see:
有关带有名称和编号的 HTML 实体的完整列表,请参阅:
W3 School's HTML Entity reference page.
回答by Ninos
Late to this, but you can also replace  
with &nbsp;
if you don't want to remember what  
means.
晚来,但你也可以替换 
使用&nbsp;
,如果你不想记住什么 
手段。