Search code examples
c#xmlxml-parsinglinq-to-xmlxelement

how to Parse XML using XElement.Parse without looking/changing Entities to characters?


Is theres a way to Parse XML using XElement.Parse that won't convert entities to a character?

example:

var xmlText = "<name><firstname>Willi&aacute;ms</firstname></name>";

and if I Parse this using the below:

var element = XElement.Parse(xmlText);
var firstname = element.Element("firstname");

after parsing, firstname value is now "Williáms"

How can I ignore those entities to change?


Solution

  • Character "&" is illegal in XML elements. "&" will generate an error because the parser interprets it as the start of an character entity.

    To be corrective, your Xml should be using CDATA:

    var xmlText = "<name><firstname><![CDATA[Willi&aacute;ms]]></firstname></name>";