ExtJs XSLTHelper 生成XSLT转换XML字符串

本文介绍了一种使用XSL对XML文档进行转换的方法,并提供了多个实用的函数来完成这一过程,包括将XML字符串转换为HTML或其他格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;

namespace CommonHelper
{
    
public static class XSLHelper
    
{
        
/// <summary>
        
/// 将第二个xml元素加到第一个的根目录下
        
/// </summary>
        
/// <param name="xml"></param>
        
/// <param name="xml2"></param>
        
/// <returns></returns>

        public static string Combind(string xml, string xml2)
        
{
            
int las = xml.LastIndexOf("</");
            
return xml.Substring(0, las) + xml2 + xml.Substring(las);
        }


        
/// <summary>
        
/// 通过xsl文件对Xelement类对象进行转换,返回转换后的串
        
/// </summary>
        
/// <param name="el"></param>
        
/// <param name="xslFile"></param>
        
/// <returns></returns>

        public static string TransferXml(this XElement el,string xslFile)
        
{
            XmlReaderSettings srs 
= new XmlReaderSettings();
            srs.ProhibitDtd 
= false;
            XslCompiledTransform xsl 
= new XslCompiledTransform();
            xsl.Load(xslFile);
            System.IO.StringWriter os 
= new System.IO.StringWriter();
            XmlReader xr
=XmlReader.Create(new System.IO.StringReader(el.ToString()));
            xsl.Transform(xr, 
null, os);
            
return os.ToString();
        }


        
/// <summary>
        
/// 通过XSL转换XML串,返回转换后的串
        
/// </summary>
        
/// <param name="xmlstr"></param>
        
/// <param name="xslFile"></param>
        
/// <returns></returns>

        public static string TransferXml(string xmlstr, string xslFile)
        
{
            XmlReaderSettings srs 
= new XmlReaderSettings();
            srs.ProhibitDtd 
= false;
            XmlReader xr2 
= XmlReader.Create(new System.IO.StringReader(xmlstr),srs);
            
return TransferXml(xr2, xslFile);
        }


        
/// <summary>
        
/// 通过XSL转换xmlreader,返回转换后的串
        
/// </summary>
        
/// <param name="xr2"></param>
        
/// <param name="xslFile"></param>
        
/// <returns></returns>

        public static string TransferXml(XmlReader xr2, string xslFile)
        
{
            XslCompiledTransform xsl 
= new XslCompiledTransform();
            XmlReaderSettings srs 
= new XmlReaderSettings();
            srs.ProhibitDtd 
= false;
            XmlReader xr 
= XmlReader.Create(xslFile, srs);
            xsl.Load(xr);
            System.IO.StringWriter os 
= new System.IO.StringWriter();
            
            xsl.Transform(xr2, 
null, os);
            xr2.Close();
            xr.Close();
            
return os.ToString();
        }


        
        
/// <summary>
        
/// 通过XSL转换XML串并直接输出到文件
        
/// </summary>
        
/// <param name="xmlstr"></param>
        
/// <param name="xslFile"></param>
        
/// <param name="outHtmlFile">物理路径</param>
        
/// <returns></returns>

        public static void TransferXml(string xmlstr, string xslFile,string outHtmlFile)
        
{
            XmlReaderSettings srs 
= new XmlReaderSettings();
            srs.ProhibitDtd 
= false;
            XmlReader xr2 
= XmlReader.Create(new System.IO.StringReader(xmlstr),srs);
            TransferXml(xr2, xslFile, outHtmlFile);
        }


        
/// <summary>
        
/// 通过XSL转换XMLreader流并直接输出到文件
        
/// </summary>
        
/// <param name="xr2"></param>
        
/// <param name="xslFile"></param>
        
/// <param name="outHtmlFile"></param>

        public static void TransferXml(XmlReader xr2, string xslFile,string outHtmlFile)
        
{
            XslCompiledTransform xsl 
= new XslCompiledTransform();
            XmlReaderSettings srs 
= new XmlReaderSettings();
            srs.ProhibitDtd 
= false;
            XmlReader xr 
= XmlReader.Create(xslFile, srs);
            XsltSettings xset 
= new XsltSettings(truetrue);
            xsl.Load(xr);
            System.IO.StreamWriter os 
= new System.IO.StreamWriter(outHtmlFile);
            
            xsl.Transform(xr2, 
null, os);
            xr2.Close();
            xr.Close();
            os.Close();
        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值