Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XElement.读取符号和特殊国家字符时的加载错误_C#_Xml_Xml Parsing - Fatal编程技术网

C# XElement.读取符号和特殊国家字符时的加载错误

C# XElement.读取符号和特殊国家字符时的加载错误,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我在从XML文件读取符号时遇到问题: 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.XmlEx

我在从XML文件读取符号时遇到问题:

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
Patrick&Phill
错误:名称不能以“”字符、十六进制值0x20开头。Xml.XmlException)引发System.Xml.XmlException:“名称不能以“”字符开头
或使用特殊葡萄牙语字符:

<Extra>Direc&ccedil;&atilde;o Assistida</Extra> (= <Extra>Direcção Assistida</Extra>)

Error: Reference to undeclared entity 'ccedil'
Direcç;ã;o Assistida(=Direcço Assistida)
错误:引用未声明的实体“ccedil”

知道如何解决这个问题吗?

恐怕您正在处理格式错误的XML

要表示符号AND,加载的数据应使用“&;“实体

ç;(ç)和ã;(ã)命名实体不是XML标准的一部分,它们更常见于HTML中(尽管它们可以通过使用DTD添加到XML中)

您可以先使用HtmlTidy整理数据,也可以编写一些东西将空的符号转换为传入文件上的实体

例如:

public string CleanUpData(string data)
{
    var r = new Regex(@"&\s");
    string output = r.Replace(data, "&amp; ");
    output = output.Replace("&ccedil;", "ç");
    output = output.Replace("&atilde;", "ã");
    return output;
}

您好,谢谢,但是在我加载XElement.xml?XElement.Parse(cleanUpdatea(File.ReadAllText(Path_xml_Data_File)))之前,我怎么做呢;