XElement.Load 读取与符号和特殊国家/地区字符时出错 [英] XElement.Load Error reading ampersand symbols and special country characters
问题描述
我在从 XML 文件中读取与符号时遇到问题:
I'm having problems reading the ampersand symbol from an XML file:
XElement xmlElements = XElement.Load(Path_Xml_Data_File);
我遇到以下错误:
<Name>Patrick & Phill</Name>
Error: Name cannot begin with the ' ' character, hexadecimal value 0x20. Xml.XmlException) A System.Xml.XmlException was thrown: "Name cannot begin with the ' ' character
或者使用特殊的葡萄牙语字符:
Or with special Portuguese characters:
<Extra>Direcção Assistida</Extra> (= <Extra>Direcção Assistida</Extra>)
Error: Reference to undeclared entity 'ccedil'
知道如何解决这个问题吗?
Any idea how to solve this problem?
推荐答案
恐怕您正在处理格式错误的 XML.
I'm afraid that you're dealing with malformed XML.
要表示与号,您加载的数据应使用&"实体.
To represent the ampersand, the data that you're loading should use the "&" entity.
ç(ç) 和 ã(ã) 命名实体不是 XML 标准的一部分,它们更常见于 HTML 中(尽管可以使用 DTD 将它们添加到 XML 中).
The ç (ç) and ã (ã) named entities are not part of the XML standard, they are more commonly found in HTML (although they can be added to XML by the use of a DTD).
您可以先使用 HtmlTidy 整理数据,或者您可以编写一些内容将裸露的 & 符号转换为传入文件中的实体.
You could use HtmlTidy to tidy up the data first, or you could write something to convert the bare ampersands into entities on the incoming files.
例如:
public string CleanUpData(string data)
{
var r = new Regex(@"&\s");
string output = r.Replace(data, "& ");
output = output.Replace("ç", "ç");
output = output.Replace("ã", "ã");
return output;
}
这篇关于XElement.Load 读取与符号和特殊国家/地区字符时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!