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

I have got an exception while parsing an XML string.

Here is the exception an error occurred while parsing entityname;. line 1 and the XML I have used is as follows ;
<Details CompanyName="Outlook & Outlook Express" ></Details>
The code is given below :
C#
StringReader strRdr = new StringReader(textBox1.Text);
XmlDocument xmlDoc = new XmlDocument();
try
{
    xmlDoc.Load(strRdr);
    XmlNode node = xmlDoc.DocumentElement.SelectSingleNode(@"/Details");
}
catch (Exception Ex)
{
    System.Windows.Forms.MessageBox.Show(Ex.Message.ToString(), "XML Parsing");
}


If someone can help me to solve this issue, that would be great.

Regards
Sebastian
Posted
Updated 17-Apr-17 13:04pm

1 solution

It is because of & in the company name. When the string is being loaded in the XMLDocument it throws the error.

You need to encode it before loading it up. Following should do:
C#
StringReader strRdr = new StringReader("<Details CompanyName=\"Outlook &amp; Outlook Express\" ></Details>");

If needed, decode it back when needed.
 
Share this answer
 

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