471,627 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

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

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
1 1785
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.

Similar topics

5 posts views Thread by TheKeith | last post: by
7 posts views Thread by Fabian Neumann | last post: by
8 posts views Thread by Colin Mc Mahon | last post: by
2 posts views Thread by genc ymeri | last post: by
2 posts views Thread by Roel Korsten | last post: by
8 posts views Thread by pamelafluente | last post: by
10 posts views Thread by pamelafluente | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.