By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,506 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,506 IT Pros & Developers. It's quick & easy.

Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does?

P: n/a
Does system.xml have any way to transofrm data with an xswl style sheet
using strings like MSXML2 does?

how to convert this to use System.XML so i do not depend on MSXML2 interop?

static public string XslTransform(string style, string strdata)
MSXML2.IXMLDOMDocument pDoc = new MSXML2.DOMDocumentClass();
pDoc.async = false;
MSXML2.IXMLDOMDocument pTemplate = new MSXML2.DOMDocumentClass();
pTemplate.async = false;
strdata = pDoc.transformNode(pTemplate);
return strdata;
Feb 21 '06 #1
Share this Question
Share on Google+
1 Reply

P: n/a
I wouldn't go *near* interop with this; the .Net classes work primarily with
streams; the answer, then, is to place your strings into streams, work with
them, and then read the data back.

The following should give you an idea; note that this implementation is 2.0
specific, as it uses the compiled transform class; to re-use this; (you
could implement in 1.1 without too much change). It also uses a static
dictionary to cache the transforms (using the xsl string as a key - not
ideal as the key could be quite large; generally I would use a file path or

Hope this helps; and note I threw this together for the post, so I haven't
tested it in all scenarios.

static void Main(string[] args) {
string xml = @"<Xml><El Val=""1""/><El Val=""2""/></Xml>",
xsl = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0""
<xsl:template match=""/Xml"">
<UL><xsl:for-each select=""El"">
<LI><xsl:value-of select=""@Val""/></LI>
Console.WriteLine(XslTransform(xml, xsl));


private static Dictionary<string,
System.Xml.Xsl.XslCompiledTransform> _transforms
= new Dictionary<string,

private static System.Xml.XmlReader GetReader(string xml) {
// note no "using" on stream; stream is owned by reader
System.IO.MemoryStream stream = new
System.IO.MemoryStream(System.Text.Encoding.UTF8.G etBytes(xml));
return XmlReader.Create(stream);

public static string XslTransform(string xml, string xsl) {
System.Xml.Xsl.XslCompiledTransform transform;
lock (_transforms) {
if (!_transforms.TryGetValue(xsl, out transform)) {
transform = new System.Xml.Xsl.XslCompiledTransform();
using (System.Xml.XmlReader reader = GetReader(xsl)) {
_transforms.Add(xsl, transform);
using(System.IO.MemoryStream stream = new
System.IO.MemoryStream()) {
// nested construct using is to ensure writer is fully
using(System.Xml.XmlWriter writer =
using(System.Xml.XmlReader reader = GetReader(xml))
System.Text.Encoding.UTF8.GetString(stream.ToArray ());

Feb 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.