473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XslTransform from one XmlDocument to another...

Hi

I am trying to transform on System.Xml.XmlDocument into another using
XslTransform without writing the object out to a file. I am guessing it
should work something like this...

public XmlDocument TransformXmlDoc(XmlDocument docToTransform, string
xsltFilePath)
{
//load the xslt
XslTransform trans = new XslTransform();
trans.Load(xsltFilePath);

//create a new xmlDoc
XmlDocument docToReturn = new XmlDocument();

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(str,Encoding.UTF8);
trans.Transform(docToTransform,null,xWriter,null);

//load the new doc from the stream
docToReturn.Load(str);

str.Close();

return docToReturn;
}

....but it doesn't. Can anyone give me any pointers as to why or how it
should be done.

Thanks

Graham
Nov 12 '05 #1
2 5992


Graham Pengelly wrote:

I am trying to transform on System.Xml.XmlDocument into another using
XslTransform without writing the object out to a file. I am guessing it
should work something like this...

public XmlDocument TransformXmlDoc(XmlDocument docToTransform, string
xsltFilePath)
{
//load the xslt
XslTransform trans = new XslTransform();
trans.Load(xsltFilePath);

//create a new xmlDoc
XmlDocument docToReturn = new XmlDocument();

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(str,Encoding.UTF8);
trans.Transform(docToTransform,null,xWriter,null);

//load the new doc from the stream
docToReturn.Load(str);

str.Close();

return docToReturn;
}

...but it doesn't. Can anyone give me any pointers as to why or how it
should be done.


I don't think you need a MemoryStream, you can Transform to an XmlReader
and pass that directly to the XmlDocument.
The following is .NET 1.1 code that works for me here:

using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class Test20040716 {
public static void Main (string[] args) {
string xslURL = @"test20040716Xsl.xml";
string xmlURL = @"test2004071601.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlURL);
XmlDocument resultDocument = (XmlDocument) Transform(xmlDocument,
xslURL);
Console.WriteLine(resultDocument.OuterXml);
}

public static IXPathNavigable Transform (
IXPathNavigable input,
string stylesheetURL
)
{
XslTransform xsltProcessor = new XslTransform();
xsltProcessor.Load(stylesheetURL);
IXPathNavigable output = null;
if (input is XmlDocument) {
XmlDocument outputXML = new XmlDocument();
outputXML.Load(xsltProcessor.Transform(input, (XsltArgumentList)
null, (XmlResolver) null));
output = outputXML;
}
else if (input is XPathDocument) {
output = new XPathDocument(xsltProcessor.Transform(input,
(XsltArgumentList) null, (XmlResolver) null));
}
return output;
}
}

Note that I have simply used IXPathNavigable as the result type as that
way you should be able to pass an XmlDocument as well as an
XPathDocument in and get the same type out.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2
Martin Honnen wrote:


Graham Pengelly wrote:

I am trying to transform on System.Xml.XmlDocument into another using
XslTransform without writing the object out to a file. I am guessing
it should work something like this...

public XmlDocument TransformXmlDoc(XmlDocument docToTransform, string
xsltFilePath)
{
//load the xslt
XslTransform trans = new XslTransform();
trans.Load(xsltFilePath);

//create a new xmlDoc
XmlDocument docToReturn = new XmlDocument();

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(str,Encoding.UTF8);
trans.Transform(docToTransform,null,xWriter,null);

//load the new doc from the stream
docToReturn.Load(str);
str.Close();

return docToReturn;
}

...but it doesn't. Can anyone give me any pointers as to why or how it
should be done.

I don't think you need a MemoryStream, you can Transform to an XmlReader
and pass that directly to the XmlDocument.
The following is .NET 1.1 code that works for me here:

using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class Test20040716 {
public static void Main (string[] args) {
string xslURL = @"test20040716Xsl.xml";
string xmlURL = @"test2004071601.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlURL);
XmlDocument resultDocument = (XmlDocument) Transform(xmlDocument,
xslURL);
Console.WriteLine(resultDocument.OuterXml);
}

public static IXPathNavigable Transform (
IXPathNavigable input,
string stylesheetURL
)
{
XslTransform xsltProcessor = new XslTransform();
xsltProcessor.Load(stylesheetURL);
IXPathNavigable output = null;
if (input is XmlDocument) {
XmlDocument outputXML = new XmlDocument();
outputXML.Load(xsltProcessor.Transform(input, (XsltArgumentList)
null, (XmlResolver) null));
output = outputXML;
}
else if (input is XPathDocument) {
output = new XPathDocument(xsltProcessor.Transform(input,
(XsltArgumentList) null, (XmlResolver) null));
}
return output;
}
}

Note that I have simply used IXPathNavigable as the result type as that
way you should be able to pass an XmlDocument as well as an
XPathDocument in and get the same type out.


Thanks Martin

That works a treat... I have used all sorts of different ways to achieve
this, my latest and 'most elegant' idea being the memorystream. I
can't believe I never realised that you can pass the result of the xslt
Transform method into Load(). doh....

Thanks again

Graham
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Meyer | last post by:
I have an application where I create an xml fragment using an XslTransform object. However, if I use the following output method, <xsl:output method="xml" version="1.0" encoding="UTF-8"...
1
by: Rodger McNab | last post by:
While parsing xpath expressions within an XSLT document the .NET XslTransform class seems to delete the first "/" after a "]" within an expression containing a "|". This problem can be demonstrated...
1
by: Geir Sørensen | last post by:
Hi, When I transform an xml-file using XslTransform, I seem to get a lot of elements of this form: <a> </a> <b> </b> But I really would like to get written out like I do when transforming...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
1
by: fawzib.rojas | last post by:
I have the following class and function: class MathExtension{ public double max(double a,double b){ Console.WriteLine("math:max("+a.ToString()+","+b.ToString()+")"); return Math.Max(a,b); } }...
2
by: KJS | last post by:
Hello, I'm receiving: 'System.Xml.Xsl.XsltException: Missing mandatory attribute 'version' After I try and run my transformation. I spent a good few days coming up with the appropriate (I think)...
0
by: Ruprict | last post by:
I am trying to transform a dataset with xslt, and it continually fails with a NullReference Exception after the transform when I try to read the result into another dataset I have tested the XML...
1
by: Praveen | last post by:
Have a common function in Javascript which do transform for all .xsl's. XSL object is loaded like this. var xslobj=new ActiveXObject("MSXML2.FreeThreadedDOMDocument.4.0"); xslobj.async = false;...
9
by: WT | last post by:
Hello, I have code created with .net 1.0 and migrated to 3.5. Form 2.0 the XslTransform class is obsolete and the vs2008 compiler generates warnings that these classes are absolete suggesting to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.