How to convert XML + XSL to HTML format in C #

Source: Internet
Author: User
Tags xsl xslt

Recently, I have been writing a small tool that can send the content stored in the XML file via Outlook mail. During the process of sending the mail, body content cannot be directly sent by reading XML data, but can be sent in HTML format (Set message. isbodyhtml = true), so I want to find a way to convert an XML file to an HTML file.

During the online investigation, many people saw how to convert HTML files into XML files. Most of them mentioned the compile compiledtransform class when they found that XML is converted to HTML, so I used the compile compiledtransform class,CodeAs follows:

Using system. xml;
Using system. xml. XSL;// UseXslcompiledtransformNamespace that must be added to the class
Using system. xml. XPath;

Namespace Magci. Test. xml. testxsl
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Xslcompiledtransform XSLT = new Xslcompiledtransform ();
XSLT. Load (pipeline file ); // Load method to load the style sheet. The external file indicates the XSLT style sheet to be loaded, such as books. XSL
Xpathdocument Xdoc = new Xpathdocument (Xmlpath ); // Use the XML data in the specified file for initialization, such as books. xml
Xmltextwriter xtwriter = new xmltextwriter (htmlfile, null ); // Create an xmltextwriter class instance in the specified file. htmlfile is the file to be written. If the file exists, cut the file and use the new content to edit it, such as books.html. The second parameter is the generated encoding method. If it is empty, write the file in UTF-8 form.
XSLT. Transform (xdoc, xtwriter ); // The transform method implements format conversion. The conversion is performed using the specified input document, and the result is output to xmltextwriter.

Xtwriter. Close ();// Write the XML to file and close the writer

}

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.