帮帮我,Stackoverflow!
我有一个简单的.NET 3.5控制台应用程序,它可以读取一些数据并发送电子邮件 . 我在XSLT样式表中表示电子邮件格式,以便我们可以轻松更改电子邮件的措辞,而无需重新编译应用程序 .
我们在应用转换时使用扩展对象将数据传递给XSLT:
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:EmailNotification="ext:EmailNotification">
这样,我们可以有这样的陈述:
Dear :
以上工作正常 . 我通过这样的代码传递对象(为简洁起见省略了一些不相关的代码):
// purely an example structure
public struct EmailNotification
{
public string FullName { get; set; }
}
// Somewhere in some method ...
var notification = new Notification("John Smith");
// ...
XsltArgumentList xslArgs = new XsltArgumentList();
xslArgs.AddExtensionObject("ext:EmailNotification", notification);
// ...
// The part where it breaks! (This is where we do the transformation)
xslt.Transform(fakeXMLDocument.CreateNavigator(), xslArgs, XmlWriter.Create(transformedXMLString));
所以, all of the above code works . 但是,我想得到一点点幻想(总是我的垮台)并传递一个集合,这样我就能做到这样的事情:
The following accounts need to be verified:
当我在扩展对象中传递集合并尝试转换时,我收到以下错误:
"Extension function parameters or return values which have Clr type 'String[]' are not supported."
或List,或IEnumerable,或我尝试传入的任何内容 .
所以,我的问题是:
How can I pass in a collection to my XSLT?
What do I put for the xsl:value-of select="" inside the xsl:for-each ?
我想做什么不可能?
Edit: After I saw the two answers below, I took Chris Hynes' code and modified it very slightly to suit my needs. Solution is as follows:
// extension object
public struct EmailNotification
{
public List StringsSetElsewhere { private get; set; }
public XPathNavigator StringsNodeSet
{
get
{
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("Strings");
xmlDoc.AppendChild(root);
foreach (var s in StringsSetElsewhere)
{
XmlElement element = xmlDoc.CreateElement("String");
element.InnerText = s;
root.AppendChild(element);
}
return xmlDoc.CreateNavigator();
}
}
}
在我的XSLT中......
工作得很完美!