XmlDocument throwing "An error occurred while parsing EntityName"
Solution 1
The file shown above is not well-formed XML because the ampersand is not escaped.
You can try with:
<Testing>
<Test>CITY & COUNTY</Test>
</Testing>
or:
<Testing>
<Test><![CDATA[CITY & COUNTY]]></Test>
</Testing>
Solution 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 "&", for C#, is really a string (it has five characters).
Does it work with double quotes?
editXml.Replace("&", "&");
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
amp; quot; apos; gt; lt; or #
(but this would still not be a perfect filtering)
Solution 3
To specify an ampersand in XML you should use &
since the ampersand sign ('&') has a special meaning in XML.
RG-3
Updated on October 04, 2020Comments
-
RG-3 over 3 years
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('&', '&'); }
But its giving me an err on inside the if statement : Too many literals.
-
RG-3 over 12 yearsI am doing something like this: string editXml = filterXml; if (editXml.Contains("&")) { editXml.Replace('&', '&'); }
-
Jeff Mercado over 12 yearsOption 2 is a bit overkill though and overall, not very useful in this case.
-
Ghislain Fourny over 12 yearsI would advise caution. This will work if you are sure that all ampersands are "not well-formed". If your input XML document is well-formed and contains entity or character references, doing so might break them (for example, a well-formed & will be changed to &amp;). If you have control over this, it might be easier to ensure well-formedness earlier in the data flow (when the content of filterXml is generated).