使用来自Web表单的Xml数据对XML进行XSLT转换 [英] XSLT Transform of XML using Xml data from a web form
问题描述
网络上有很多使用XSLT文件将XML文件转换为其他格式的示例,如下所示:
There are lots of examples on the web of transforming an XML file to a different format using an XSLT file, like the following:
XslTransform myXslTransform = new XslTransform();
XsltSettings myXsltSettings = new XsltSettings();
myXsltSettings.EnableDocumentFunction = true;
myXslTransform.Load("transform.xsl");
myXslTransform.Transform("input.xml", "output.xml");
但是,这只是部分答案,我希望能够从Web表单获取XML输入数据,并将其用作输入xml数据而不是".xml"文件,但没有找到任何具体答案.例子.使用Visual Studio,我看到了接受XmlReader对象作为参数的Load方法,但是我不知道如何使用表单和TextBox控件中的数据来创建其中之一.如果有人可以提供使用表单数据而不是输入文件来转换XML的示例,那将非常有帮助.
However this is only a partial answer, I would like to be able to get the XML input data from a web form and use that as the input xml data instead of an '.xml' file, but have not found any concrete examples. Using Visual Studio I see Load methods that accept XmlReader objects as parameters but I do not know how to create one of those using the data from a form and TextBox control. It would be very helpful if someone could provide an example of transforming XML using form data instead of an input file.
推荐答案
请注意,自.NET 2.0开始, XslTransform
已过时,应改为使用 XslCompiledTransform
.如果要使用 XslSettings
,请确保将其传递给 XslCompiledTransform
的 Load
方法(例如
Please note that XslTransform
is obsolete since .NET 2.0, you should use XslCompiledTransform
instead. And if you want to use XslSettings
then make sure you pass them in to the XslCompiledTransform
's Load
method (e.g. http://msdn.microsoft.com/en-us/library/ms163425.aspx), simply creating it does not make sense.
对于解析XML,您可以在字符串变量或属性(例如 TextBox
的 Text
属性)中使用很多选项,可以使用 XmlReader
在 StringReader
上,例如
As for parsing XML you have in a string variable or property (like the Text
property of a TextBox
) you have lots of options, you can use an XmlReader
over a StringReader
e.g.
XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("sheet.xsl");
using (StringReader sr = new StringReader(TextBox1.Text))
{
using (XmlReader xr = XmlReader.Create(sr))
{
proc.Transform(xr, null, Response.Output);
}
}
或者您可以从字符串创建 XPathDocument
或 XmlDocument
或 XDocument
并使用 Transform
方法,该方法将 IXPathNavigable
作为第一个参数.
Or you can create an XPathDocument
or XmlDocument
or XDocument
from the string and use an overload of the Transform
method that takes an IXPathNavigable
as the first argument.
这篇关于使用来自Web表单的Xml数据对XML进行XSLT转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!