ExtJs XSLTHelper 生成XSLT转换XML字符串

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();
        }

    }

}

 

猜你喜欢

转载自blog.csdn.net/baozhenliang/article/details/2172389
今日推荐