XmlDocument抛出“解析EntityName时发生错误”

时间:2021-06-11 18:26:51

I have a function where I am passing a string as params called filterXML which contains '&' in one of the properties.

我有一个函数,在其中一个属性中传递一个名为filterXML的字符串作为params,其中包含'&'。

I know that XML will not recognize it and it will throw me an err. Here is my code:

我知道XML不会识别它,它会让我犯错误。这是我的代码:

 public XmlDocument TestXMLDoc(string filterXml)
{
    XmlDocument doc = new XmlDocument();
    XmlNode root = doc.CreateElement("ResponseItems");

    // put that root into our document (which is an empty placeholder now)
    doc.AppendChild(root);

    try
    {
        XmlDocument docFilter = new XmlDocument();
        docFilter.PreserveWhitespace = true;

        if (string.IsNullOrEmpty(filterXml) == false)
            docFilter.LoadXml(filterXml); //ERROR THROWN HERE!!!

What should I change in my code to edit or parse filterXml? My filterXml looks like this:

我应该在代码中修改什么来编辑或解析filterXml?我的filterXml是这样的:

<Testing>
<Test>CITY & COUNTY</Test>
</Testing>

I am changing my string value from & to &. Here is my code for that:

我正在将字符串值从&改为&。这是我的代码:

string editXml = filterXml;
    if (editXml.Contains("&"))
    {
        editXml.Replace('&', '&amp;');
    }

But its giving me an err on inside the if statement : Too many literals.

但它让我在if语句中犯了一个错误:太多的文字。

3 个解决方案

#1


13  

The file shown above is not well-formed XML because the ampersand is not escaped.

上面所示的文件不是格式良好的XML,因为它没有被转义。

You can try with:

你可以尝试:

<Testing>
  <Test>CITY &amp; COUNTY</Test>
</Testing>

or:

或者:

<Testing>
  <Test><![CDATA[CITY & COUNTY]]></Test>
</Testing>

#2


2  

About the second question: there are two signatures for String.Replace. One that takes characters, the other that takes strings. Using single quotes attempts to build character literals - but "&amp;", for C#, is really a string (it has five characters).

关于第二个问题:字符串有两个签名。一个接收字符,另一个接收字符串。使用单引号尝试构建字符文字-但是"&"对于c#来说,它实际上是一个字符串(它有五个字符)。

Does it work with double quotes?

它能用双引号吗?

editXml.Replace("&", "&amp;");

If you would like to be a bit more conservative, you could also write code to ensure that the &s you are replacing are not followed by one of

如果您想要稍微保守一点,您也可以编写代码以确保您正在替换的&s没有被其中的一个所替代。

amp; quot; apos; gt; lt; or #

(but this would still not be a perfect filtering)

(但这仍不是完美的过滤)

#3


1  

To specify an ampersand in XML you should use &amp; since the ampersand sign ('&') has a special meaning in XML.

要在XML中指定& & & & &;因为&符号在XML中有特殊的含义。

#1


13  

The file shown above is not well-formed XML because the ampersand is not escaped.

上面所示的文件不是格式良好的XML,因为它没有被转义。

You can try with:

你可以尝试:

<Testing>
  <Test>CITY &amp; COUNTY</Test>
</Testing>

or:

或者:

<Testing>
  <Test><![CDATA[CITY & COUNTY]]></Test>
</Testing>

#2


2  

About the second question: there are two signatures for String.Replace. One that takes characters, the other that takes strings. Using single quotes attempts to build character literals - but "&amp;", for C#, is really a string (it has five characters).

关于第二个问题:字符串有两个签名。一个接收字符,另一个接收字符串。使用单引号尝试构建字符文字-但是"&"对于c#来说,它实际上是一个字符串(它有五个字符)。

Does it work with double quotes?

它能用双引号吗?

editXml.Replace("&", "&amp;");

If you would like to be a bit more conservative, you could also write code to ensure that the &s you are replacing are not followed by one of

如果您想要稍微保守一点,您也可以编写代码以确保您正在替换的&s没有被其中的一个所替代。

amp; quot; apos; gt; lt; or #

(but this would still not be a perfect filtering)

(但这仍不是完美的过滤)

#3


1  

To specify an ampersand in XML you should use &amp; since the ampersand sign ('&') has a special meaning in XML.

要在XML中指定& & & & &;因为&符号在XML中有特殊的含义。