【问题标题】:Disable the reference to entity with XML使用 XML 禁用对实体的引用
【发布时间】:2009-06-04 02:28:34
【问题描述】:

我目前正在开发必须从 XML 流中读取数据的程序,该数据有时可能包含“&”符号

即:

<XMLRoot>
    <Element>
        <Node>
            This is a dummy data&more!
        </Node>
    </Element>
</XMLRoot>

当我解析此文本时,我收到一条错误消息,告诉我“对未声明实体的引用”。

有没有办法使用 C# 的 XMLParser 删除“实体检查”?

【问题讨论】:

  • 问题是您面临的 XML 格式错误,根据 XML 规范,与号字符 (&) 应转义为 &

标签: c# .net xml xmldocument


【解决方案1】:

我认为问题在于您发布的 XML 是格式错误的 XML,“&”是 XML 中的保留字符,需要转义或包含在 CDATA 部分中:

<XMLRoot>
<Element>
    <Node>
        This is a dummy data&amp;more!
    </Node>
</Element>
</XMLRoot>

或者:

<XMLRoot>
<Element>
    <Node>
        <![CDATA[ This is a dummy data&more! ]]>
    </Node>
</Element>
</XMLRoot>

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2010-09-21
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多