Search code examples
c#xmlxmldocument

Read special characters back from XmlDocument in c#


Say I have the an xml with a escaped ampersand (&). How do I then read it back such that the result gives me the 'un-escaped' text.

Running the following gives me "&amp" as the result. How do I get back '&'

void Main()
{
  var xml = @"
  <a>
    &amp;
  </a>
  ";
  var doc = new XmlDocument();
  doc.LoadXml(xml);
  var ele = (XmlElement)doc.FirstChild;
  Console.WriteLine (ele.InnerXml); 
}

Solution

  • Use ele.InnerText instead of ele.InnerXml