An error occurred while parsing EntityName. Line1, position 844

C#Xml

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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;");
  }

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

You can use: SecurityElement.Escape(XML string)

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSebastian XavierView Question on Stackoverflow
Solution 1 - C#Bassam AlugiliView Answer on Stackoverflow
Solution 2 - C#AlastairView Answer on Stackoverflow
Solution 3 - C#DJLView Answer on Stackoverflow
Solution 4 - C#CodorView Answer on Stackoverflow
Solution 5 - C#Pavan JoshiView Answer on Stackoverflow