Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlDocument throwing "An error occurred while parsing EntityName"

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

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

 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:

<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.

like image 297
RG-3 Avatar asked Oct 03 '11 17:10

RG-3


1 Answers

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

You can try with:

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

or:

<Testing>
  <Test><![CDATA[CITY & COUNTY]]></Test>
</Testing>
like image 199
Ghislain Fourny Avatar answered Nov 04 '22 12:11

Ghislain Fourny