locked
Encoding HTML data in an XML file RRS feed

  • Question

  • User1064403333 posted

    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:

    myDataTable.DataSet.WriteXml(filePath, XmlWriteMode.IgnoreSchema);
    

    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:

    <h1>Hello World</h1>

    Questions

    1. From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?

    2. Should I also use HttpUtility.HtmlEncode, or is this redundant i.e. double encoding?

    Thanks.

    Tuesday, November 24, 2015 12:46 AM

Answers

  • User-821857111 posted

    From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?
    Yes.
    Should I also use HttpUtility.HtmlEncode
    No. There is no need. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2015 7:12 AM
  • User-271186128 posted

    Hi Sir,

    Questions

    1. From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?

    2. 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.

     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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2015 8:02 AM

All replies

  • User-821857111 posted

    From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?
    Yes.
    Should I also use HttpUtility.HtmlEncode
    No. There is no need. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2015 7:12 AM
  • User-271186128 posted

    Hi Sir,

    Questions

    1. From the first test result, I am assuming the XmWriter is already doing encoding on the HTML content internally / automatically?

    2. 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.

     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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2015 8:02 AM
  • User1064403333 posted

    Dillion and Mike

    I needed a sanity check on what seems obvious.

    Thanks for your help!

    Tuesday, November 24, 2015 8:29 PM