Preserve XML entity references while getting InnerText or InnerXml with XmlDocument

849 views Asked by At

I am trying to get InnerText or InnerXml of an Xml node using XmlDocument, at the same time I want to preserve & ' ".

However, even if I try InnerXml, it only preserves & but not ' and "

I would like to know the exact reason why it is happening and the solution to this problem.

e.g. Suppose I have an Xml node with text inside "'Cancel' & 'Abort'" and in retrun when I read this node with InnerXml (or InnerText), it must give me the exact same as a string output.

But when I try to do this, it returns, "'Cancel' & 'Abort'" This means it only preserves &

Note:

<source>&quot;&amp;Cancel&apos;&quot;</source>
<target>&quot;&amp;Cancel&apos;&quot;</target>

This is something I have in my XLIFF(XML) file, so while reading this XLIFF(XML) file, I want to retrieve the text inside both the nodes as they are visible now, without any exclusive conversion or any processing.

The Problem is the XLIFf(XML) data that I have, contains both ' or " as well as &apos; and &quot; and my goal is to detect these entity references as an error because we are not supposed to have that in our data. In other words, only ' or " shall be permitted in our data.

1

There are 1 answers

3
NaYaN On

Try using WebUtility.HtmlEncode() method on your InnerXML or InnerText