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(true, true);
xsl.Load(xr);
System.IO.StreamWriter os = new System.IO.StreamWriter(outHtmlFile);
xsl.Transform(xr2, null, os);
xr2.Close();
xr.Close();
os.Close();
}
}
}