53

I have got the following exception from the below code block.

An error occurred while parsing EntityName. Line1, position 844.

I was trying to parse s set of data retrieved from table to a data set.

public DataSet BindMasterData(string xml)
        {
            DataSet ds = null;
            try
            {
                ds = new DataSet();
                TextReader txtReader = new StringReader(xml);
                XmlReader reader = new XmlTextReader(txtReader);
                ds.ReadXml(reader);
            }
            catch (Exception ex)
            {
                return new DataSet();
            }
            return ds;
        }

I have figured out the reason for the exception, but I couldn't solve it. In this particular situation, the string(which is retrieved from DB) contains a special character (&). That causes exception. How I can solve it. Any help on this would be great.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Sebastian Xavier
  • 2,499
  • 7
  • 27
  • 41

5 Answers5

96

Just replace them:

Not valid in XML elements:

"   "
'   '
<   &lt;
>   &gt;
&   &amp;

  public static string UnescapeXMLValue(string xmlString)
  {
    if (xmlString == null)
        throw new ArgumentNullException("xmlString")
    
    return xmlString.Replace("&apos;", "'").Replace("&quot;", "\"").Replace("&gt;", ">").Replace("&lt;", "<").Replace("&amp;", "&");
  }
 
 public static string EscapeXMLValue(string xmlString)
  {

    if (xmlString == null)
        throw new ArgumentNullException("xmlString")

    return xmlString.Replace( "&","&amp;").Replace("'","&apos;").Replace( "\"", "&quot;").Replace(">","&gt;").Replace( "<","&lt;");
  }
Siddhant
  • 1,074
  • 12
  • 22
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • 5
    If you string the Replace function like that you can easily end up causing an error. e.g. "Dave's" would first become "Dave's" and would then become "Dave&apos;s" which would display as "Dave's". This is seen frequently on some websites because someone made a mistake. I would suggest you don't try it using replace but instead look at http://stackoverflow.com/questions/8331119/escape-invalid-xml-characters-in-c-sharp – Dave Williams Oct 01 '14 at 13:40
  • 3
    Replace( "&","&") should be call first in your method, otherwise something would happen like ">" turns into "&gt;", it would be wrong. – William Lee Oct 22 '19 at 09:50
  • 1
    `return xmlString.Replace("'","'").Replace( "\"", """).Replace(">",">").Replace( "<","<").Replace( "&","&");` - the last part will find the previously inserted `&` and replace them with `&amp`. – Ivan Ičin Feb 29 '20 at 18:18
  • Replacing `"&"` with `"&"` gave me the same error; only once I replaced with a regular string the parsing worked – half of a glazier Mar 01 '20 at 12:56
25

This has already been answered, but found a nicer way of achieving the same outcome by doing this in .NET 4.5 by using the Escape method as below:

var xmlWithEscapedCharacters = SecurityElement.Escape(xmlWithoutEscapedCharacters);

and then just plug that string into the XML that is being generated.

Link: MSDN - SecurityElement.Escape Method

Alastair
  • 350
  • 6
  • 9
  • 2
    Also worth noting this is also available for .NET Core https://apisof.net/catalog/System.Security.SecurityElement.Escape(String) – Alastair Dec 15 '16 at 15:24
1

If your XML is being constructed with string concatenation then you'll be wanting to escape it. & should become &amp; while < and > should become &lt; and &gt; respectively.

There may be others too. Of course you should really use a proper XML encoder rather than trying to do it yourself for many reasons.

DJL
  • 2,060
  • 3
  • 20
  • 39
1

As the data comes from some data base, the problem can be hot-fixed in the context you described, but I would strongly recommend a different approach. The ampersand symbol should not have occured in the xml in the first place, as discussed here. Please clarify how the xml is generated and address the problem there.

Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
1

You can use: SecurityElement.Escape(XML string)

Reference link: https://msdn.microsoft.com/en-us/library/system.security.securityelement.escape(v=vs.110).aspx

Blue
  • 22,608
  • 7
  • 62
  • 92
  • Unfortinatly it make the same problem as common replace. I tested it like var tagText = SecurityElement.Escape("jsskhfkj & nln &"); and get - "jsskhfkj & nln &amp;". So you see that & prodeses "&amp;". – Kate May 25 '20 at 15:30