Converting XSLT File to HTML with C#

How to Convert XSLT File to HTML with C#?


In this article, we will show you how to convert your xslt files to html. Since web technologies are widely used today, xslt files must be translated as html in order for browsers to read them. Now let's look at this process, which can actually be done easily with C#.

 

  /// <summary>
        /// Xml'i Html koduna çevirir.
        /// </summary>
        /// <param name="xslEncoded"></param>
        /// <param name="inputXml"></param>
        /// <returns></returns>
        public static string XmlToHtml(string xslEncoded, string inputXml)
        {
            byte[] data = System.Convert.FromBase64String(xslEncoded);
            string decodedXslt = Encoding.UTF8.GetString(data);
            string xmlFile = File.ReadAllText(inputXml);
            return TransformXMLToHTML(xmlFile, decodedXslt);
        }

        public static string TransformXMLToHTML(string inputXml, string xsltString)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            using (XmlReader reader = XmlReader.Create(new StringReader(xsltString)))
            {
                transform.Load(reader);
            }
            StringWriter results = new StringWriter();
            using (XmlReader reader = XmlReader.Create(new StringReader(inputXml)))
            {
                transform.Transform(reader, null, results);
            }
            return results.ToString();
        }

 

 

The XmlToHtml method takes 2 parameters. These are xslEncoded and inputXml parameters. The xslEncoded parameter is where the xslt file will arrive as a base64 string, and inputXml is the path to the xml file to be translated.

The TransformXMLToHTML method processes these 2 parameters and converts them to HTML. The xslt sent when converting to html is important. Because thanks to xslt, the xml file can be styled and it helps convert it into html.

We use this method to convert e-invoices, e-archives and e-delivery notes into HTML codes. If you are going to work on e-transformation, you can make your job easier with these useful code pieces. You can contact us with any of your needs, we would like to help you.

We wish everyone good work.

 

Would you like to have an interview?

We help you with your business's most critical issues and opportunities. How about getting permanent change and results together?