What's the best way to apply an XSL transform to an XElement?
I have a document loaded via XElement.Parse() and I need to apply a XSL transform to it. How would I go about doing this? The only way I know to do is with XPathDocument and I know no way to convert between the two other than XEle开发者_如何学编程ment->String->StringReader->Stream->XPathDocument.
There must be a better way.
In a related question, what's the best way to apply XPath to an XElement?
Why not use:
xslt.Transform(xelement.CreateReader(), arglist, someTextWriter);
where xelement
is an XElement
, xslt
is an XslCompiledTransform
, arglist
is an XsltArgumentList
and someTextWriter
is a TextWriter
?
In other words, use this instance method: XElement.CreateReader()
Or, you can use:
xslt.Transform(xelement.CreateNavigator(null), someXmlWriter);
Read more about the CreateNavigator()
extension method here and here.
The last MSDN link contains a complete code example of an XSLT transformation.
Update: This additional question was asked:
In a related question, what's the best way to apply XPath to an
XElement
?
There are different ways to evaluate an XPath expression over an XNode
Extensions.XPathEvaluate()
or
Extensions.XPathSelectElements()
or
Extensions.XPathSelectElement()
精彩评论