腾格里

程序员交流学习的网站

C# 实现xlst模板生成静态html

因为html丰富美观的渲染效果,我们需要通过htmL来展示数据,例如发送html文本的邮件,往往这类场景html内容大致一直,只是数据一样。在这种情况下,可以使用xlst模板生成静态html文件。
下面是C# Xlst Helper工具。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Xsl;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp4
{
/// <summary>
/// xslt文件模板转html heler class
/// </summary>
public class XsltHelper
{

    public static string Transform(string xsltPath, Dictionary<string, string> argumentDic)
    {
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(xsltPath);
        XsltArgumentList arglist = new XsltArgumentList();
        foreach (var d in argumentDic)
        {
            arglist.AddParam(d.Key, string.Empty, d.Value);
        }

        using (XmlReader xml = XmlReader.Create(new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root></root>")))
        using (MemoryStream reader = new MemoryStream())
        {
            transform.Transform(xml, arglist, reader);
            return Encoding.UTF8.GetString(reader.ToArray());

        }

    }

    /// <summary>
    /// xslt文件转html,支持html table
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="xsltPath"></param>
    /// <param name="argumentDic"></param>
    /// <param name="tableArgs">html table数据</param>
    /// <returns></returns>
    public static string Transform<T>(string xsltPath, Dictionary<string, string> argumentDic, List<T> tableArgs)
    {
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(xsltPath);
        XsltArgumentList arglist = new XsltArgumentList();

        if (argumentDic != null)
        {
            foreach (var d in argumentDic)
            {
                arglist.AddParam(d.Key, string.Empty, d.Value);
            }
        }
        

        MemoryStream sm = new MemoryStream(XmlSerialize(tableArgs));

        using (XmlReader xml = XmlReader.Create(sm))
        using (MemoryStream reader = new MemoryStream())
        {              
            transform.Transform(xml, arglist, reader);
            sm.Close();
            return Encoding.UTF8.GetString(reader.ToArray());
        }

    }

         public static byte[] XmlSerialize<T>(T obj)
        {

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            string xmlString = string.Empty;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), new XmlRootAttribute("root"));
            using (MemoryStream ms = new MemoryStream())
            {

                xmlSerializer.Serialize(ms, obj, ns);
                return ms.ToArray();
            }

        }
    }
}

使用方式:

static void Main(string[] args)
    {
        string xsltPath = Environment.CurrentDirectory+"\\template.xslt";

        Dictionary<string, string> argumentDic = new Dictionary<string, string>();
        argumentDic.Add("user_name", "王二");

        List<User> list = new List<User>();
        list.Add(new User { UserName = "张三", Age = 23 });
        list.Add(new User { UserName = "李四", Age = 34 });

        string html = XsltHelper.Transform<User>(xsltPath, argumentDic, list);

        Console.WriteLine(html);
        Console.Read();

    }

下载Demo

未经允许不得转载:腾格里 » C# 实现xlst模板生成静态html

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. 可以用 谢谢

    你好 (2018-12-27) 回复