locked
How to add html tag into XmlTextWriter RRS feed

  • Question

  • User689330941 posted

    Hi,

     I will like to add the following html tags into the XMLTextWriter. How do I proceed to code it? Appreciate assistance.

    <![CDATA[
    <!doctype html>
    <html lang="en" prefix="op: http://media.facebook.com/op#">
    <head>
    <meta charset="utf-8">
    <link rel="canonical" href=http://abc.com>

    Thursday, April 6, 2017 4:10 AM

All replies

  • User-707554951 posted

    Hi snowfrost88,

    You could try to use the following code:

       XmlTextWriter writer = new XmlTextWriter("titles.xml", null);
                writer.Formatting = Formatting.Indented;
                writer.WriteCData("You Value Here");
                writer.WriteRaw("<!doctype html>");
                writer.WriteStartElement("html");
                writer.WriteAttributeString("lang", "en");
                writer.WriteAttributeString("prefix", "op: http://media.facebook.com/op#");
                writer.WriteEndElement();
                writer.WriteStartElement("head");
                writer.WriteEndElement();
                writer.WriteRaw("<meta charset='utf - 8' />");
                writer.WriteRaw("<link rel='canonical' href='http://abc.com>'");    

    For the usage of XmlTextWriter. You could refer to the following links:

    http://stackoverflow.com/questions/1596452/how-do-you-get-an-xmlwriter-to-write-an-html-tag-with-xmlns-and-xmllang

    https://msdn.microsoft.com/en-us/library/wkee9k2s%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396

    Best regards

    Cathy

    Friday, April 7, 2017 3:15 AM
  • User689330941 posted

    Hi Cindy,

     What if I want to add some content into the XML but my content contains of html tag? Can I just put writer.WriteRaw("<br>"+ content+"</br>"); ? Will the XML be able to display the following instead of escape character in the XML format?

    <br>

    <strong>test</strong>

    </br>

    I want to compose the xml -Article node as indicated in this link: https://github.com/jkiss/Instant-Article-Dev-Help-Doc/blob/master/Instant-Article-RSS-example.rss but I keep getting the escape character when my content is displayed in XML. My content is storing the html tag in database.

    Friday, April 7, 2017 3:40 AM
  • User1771544211 posted

    Hi snowfrost,

    I want to compose the xml -Article node as indicated in this link:

    I checked the content in the link, you can use the XMLWriter.WriteCData method to write CData. The following code shows how to do this, please take it as reference.

        //use StringBuilder to format the html content
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<html>");
        sb.AppendLine("<body>");
        sb.AppendLine("<p>Test Content</p>");
        sb.AppendLine("<div>Test Div Content</div>");
        sb.AppendLine("</body>");
        sb.AppendLine("</html>");
    
        using (XmlWriter writer = XmlWriter.Create(@"D:\\test.xml"))
        {
            writer.WriteStartElement("root");
            writer.WriteCData(sb.ToString());
            writer.WriteEndElement();
        }

    The result in the xml file of above code:

    <?xml version="1.0" encoding="utf-8"?><root><![CDATA[<html>
    <body>
    <p>Test Content</p>
    <div>Test Div Content</div>
    </body>
    </html>
    ]]></root>

    Best Regards,

    Jean

    Wednesday, April 26, 2017 6:49 AM