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
}
}
}