Reference to undeclared entity exception while working with XML

Rob picture Rob · Nov 11, 2008 · Viewed 30.2k times · Source

I am trying to set the innerxml of a xmldoc but get the exception: Reference to undeclared entity

XmlDocument xmldoc = new XmlDocument();
string text = "Hello, I am text α   – —"
xmldoc.InnerXml = "<p>" + text + "</p>";

This throws the exception:

Reference to undeclared entity 'alpha'. Line 2, position 2..

How would I go about solving this problem?

Answer

Stephan Leclercq picture Stephan Leclercq · Nov 11, 2008

XML, unlike HTML does not define entities (ie named references to UNICODE characters) so &alpha; &mdash; etc. are not translated to their corresponding character. You must use the numerical value instead. You can only use &lt; and &amp; in XML

If you want to create HTML, use an HtmlDocument instead.