我在我的网站项目中有一个xml文件和一个xslt文件。通过调用xslt中的C#函数更新xml文件
XML文件:
<root>
<employee>
<firstname>Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:Concat="urn:XslSample">
<xsl:output method="html" indent="yes"/>
<xsl:template match="root">
<xsl:for-each select="employee">
<![CDATA[Concatenated name is ]]>
<xsl:value-of select="Concat:GetFullName(firstname,lastname)"/>
<xsl:value-of select="age"/>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
MemoryStream objStream = new MemoryStream();
StreamWriter objWriter = new StreamWriter(objStream, System.Text.Encoding.UTF8);
XPathDocument doc = new XPathDocument(Server.MapPath("XMLFile.xml"));
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(Server.MapPath("XSLTFile.xslt"));
//create the XslArgumentList and new BookUtils object
XsltArgumentList argList = new XsltArgumentList();
Concat objUtil = new Concat();
//this tells the argumentlist about BookUtils
argList.AddExtensionObject("urn:XslSample", objUtil);
//new XPathNavigator
XPathNavigator nav = doc.CreateNavigator();
//do the transform
trans.Transform(nav, argList, objWriter);
//objWriter.Flush();
objStream.Position = 0;
StreamReader oReader = new StreamReader(objStream);
string strResult = oReader.ReadToEnd();
//objWriter.Close();
//oReader.Close();
Response.Write(strResult);
}
catch (Exception Ex)
{ Response.Write(Ex.Message); }
}
}
public class Concat
{
public Concat()
{ }
public string GetFullName(string firstname, string lastname)
{ return "Mr." + firstname; }
}
当我运行的网站,我需要从XSLT调用C#功能修改xml文件中的值....我通过ac#代码在每个firstname前添加一个文本(称为“Mr.”)....添加后,它将输出t作为响应,但是原始的xml没有被修改。我想,要反映在XML文件....
XML输出需要:
<root>
<employee>
<firstname>Mr.Kaushal</firstname>
<lastname>Parik</lastname>
</employee>
<employee>
<firstname>Mr.Abhishek</firstname>
<lastname>Swarnkar</lastname>
</employee>
</root>
此外,作为下一步,我需要添加另一个节点的XML文件(比如年龄)通过另一个c#函数....请注意,应该从我的xslt文件调用c#函数....任何人都可以帮助我用这个简单的代码?需要
最终的XML:
<root>
<employee>
<firstname>Mr.Kaushal</firstname>
<lastname>Parik</lastname>
<age>34</age>
</employee>
<employee>
<firstname>Mr.Abhishek</firstname>
<lastname>Swarnkar</lastname>
<age>30</age>
</employee>
</root>
可能的重复[通过在xslt中调用C#函数来改变xml文件](http://stackoverflow.com/questions/11772063/alter-xml-file-by-calling-ac-sharp-function-in-xslt ) – 2012-08-02 12:35:56