xml文件: <?xml version="1.0" encoding="utf-8" ?><order> <book ISBN="10-861003-324"> <title>the handmaid's tale</title> <price>19.95</price> </book> <cd ISBN="2-3631-4"> <title>americana</title> <price>16.95</price> </cd></order> xslt文件: <?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="date"/> <xsl:template match="/"> <order> <date> <xsl:value-of select="$date"/> </date> <total> <xsl:value-of select="sum(//price)"/> </total> </order> </xsl:template></xsl:stylesheet> 主程序: using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Xml.Xsl;using System.Xml.XPath; namespace argumentlistxslt{ class Program { private static string inputfile = @"E:\c#\c#_windows程序设计\argumentlistxslt\argumentlistxslt\order.xml"; private static string stylefile = @"E:\c#\c#_windows程序设计\argumentlistxslt\argumentlistxslt\order.xslt"; private static string resultfile = @"E:\c#\c#_windows程序设计\argumentlistxslt\argumentlistxslt\currentorder.xml"; static void Main(string[] args) { XPathDocument inputdoc=new XPathDocument(inputfile); XslCompiledTransform transformer=new XslCompiledTransform(); transformer.Load(stylefile); //创建xsltargumentlist 参数 XsltArgumentList arglist=new XsltArgumentList(); DateTime rightnow=DateTime.Now; arglist.AddParam("date","",rightnow.ToString()); //中间“” 表示默认命名空间 XmlTextWriter xtw=new XmlTextWriter(resultfile,null); transformer.Transform(inputdoc,arglist,xtw); xtw.Close(); } }}

评论