Transform Result Doc as XDocument
-
Friday, April 27, 2012 8:29 PM
XPathDocument xpathDoc = new XPathDocument(sourcePath); XPathDocument xpathTransform = new XPathDocument(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl"))); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(xpathTransform); trans.Transform(xpathDoc, writer);How do you output an XDocument from a transform?
The above code sends the output to an XmlTextWriter. But, I need the output as an XDocument or, at least, an XmlDocument.
All Replies
-
Friday, April 27, 2012 10:38 PM
What is writer? XmlWriter? Then something like
MemoryStream stream = new MemoryStream(); XmlWriter writer = XmlWriter.Create(stream); XPathDocument xpathDoc = new XPathDocument(sourcePath); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl"))); trans.Transform(xpathDoc, writer); XDocument doc = XDocument.Load(stream);
Or simplyMemoryStream stream = new MemoryStream(); XPathDocument xpathDoc = new XPathDocument(sourcePath); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl"))); trans.Transform(xpathDoc, null, stream); XDocument doc = XDocument.Load(stream); -
Saturday, April 28, 2012 9:17 AM
Here is a short example:
XslCompiledTransform proc = new XslCompiledTransform(); proc.Load("sheet.xslt"); XDocument result = new XDocument(); using (XmlWriter xw = result.CreateWriter()) { proc.Transform("input.xml", xw); xw.Close(); } // now use XDocument result here e.g. for testing check output Console.WriteLine(result);
MVP Data Platform Development My blog
- Proposed As Answer by Martin Honnen Saturday, April 28, 2012 9:17 AM
- Marked As Answer by Newmanb1 Tuesday, May 01, 2012 3:25 PM

