XElement.Load 读取与符号和特殊国家/地区字符时出错 [英] XElement.Load Error reading ampersand symbols and special country characters

查看:11
本文介绍了XElement.Load 读取与符号和特殊国家/地区字符时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 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&ccedil;&atilde;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.

要表示与号,您加载的数据应使用&amp;"实体.

To represent the ampersand, the data that you're loading should use the "&amp;" entity.

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

The &ccedil; (ç) and &atilde; (ã) 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, "&amp; ");
    output = output.Replace("&ccedil;", "ç");
    output = output.Replace("&atilde;", "ã");
    return output;
}

这篇关于XElement.Load 读取与符号和特殊国家/地区字符时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆