From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?
Should I also use HttpUtility.HtmlEncode, or is this redundant i.e. double encoding?
Yes, you are right, the XmlWriter method will automatically encode the html content. Because, if it's not encoding the content, the output XML file might be like this:
<h1> Hello World </h1>
Thus, when you read this xml file, it will be read as an xml node, instead the text content.
Keep it Simple
Should I also use HttpUtility.HtmlEncode, or is this redundant i.e. double encoding?
There is no need to use it. The XmlWriter method will automatically encode it.
Best regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
247 Points
315 Posts
Encoding HTML data in an XML file
Nov 24, 2015 12:46 AM|Keep it Simple|LINK
Hi
I have a DataTable, where one of its columns will have clean valid HTML input from the administrator only.
I am using the following code to write to an XML file:
when I open the XML file, the column containing the HTML content, seems to be encoded:
<h1>Hello World</h1>
Also, in another test, I have explicitly applied HttpUtility.HtmlEncode() on the HTML column before calling the WriteXml method.
When I open the XML file again, the column containing the HTML, seems to be encoded similarly:
&lt;h1&gt;Hello World&lt;/h1&gt;
Questions
From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?
Should I also use HttpUtility.HtmlEncode, or is this redundant i.e. double encoding?
Thanks.
All-Star
194490 Points
28079 Posts
Moderator
Re: Encoding HTML data in an XML file
Nov 24, 2015 07:12 AM|Mikesdotnetting|LINK
All-Star
45489 Points
7008 Posts
Microsoft
Re: Encoding HTML data in an XML file
Nov 24, 2015 08:02 AM|Zhi Lv - MSFT|LINK
Hi Sir,
Yes, you are right, the XmlWriter method will automatically encode the html content. Because, if it's not encoding the content, the output XML file might be like this:
Thus, when you read this xml file, it will be read as an xml node, instead the text content.
There is no need to use it. The XmlWriter method will automatically encode it.
Best regards,
Dillion
Member
247 Points
315 Posts
Re: Encoding HTML data in an XML file
Nov 24, 2015 08:29 PM|Keep it Simple|LINK
Dillion and Mike
I needed a sanity check on what seems obvious.
Thanks for your help!