Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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.

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

Thanks & Regards
Sebastian
Posted
Comments
Sanket Saxena 8-May-14 8:45am    
Could you paste the string generated (including & as you said)?
Sebastian T Xavier 9-May-14 2:15am    
added the code below & solved it....
if (xml != null)
{
xml = xml.Replace("&", "&");
}

1 solution

You need to format your xml properly. An & should actually be & So, format the xml properly when you pass it into this function.

If for some reason you cannot control the xml getting passed to you, you can turn off character checking by doing something like this:
C#
string filePath = @"You file dirtest.xml"; XmlReaderSettings xrs = new XmlReaderSettings(); xrs.CheckCharacters = false; using (XmlReader reader = XmlReader.Create(filePath, xrs)) { while (reader.ReadToFollowing("Content")) {} } - See more at: http://www.debugdotnet.com/xmlreader-read-special-unicode-use-xmlreadersettings-checkcharacters.html#sthash.HrQMOFOd.dpuf

http://www.debugdotnet.com/xmlreader-read-special-unicode-use-xmlreadersettings-checkcharacters.html[^]

However, the best option is to send in properly formed xml.
 
Share this answer
 
Comments
Sebastian T Xavier 9-May-14 2:16am    
Thanks RyanDev....

Added the below code and solved this issue...

if (xml != null)
{
xml = xml.Replace("&", "&");
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900