473,735 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XslTransform from one XmlDocument to another...

Hi

I am trying to transform on System.Xml.XmlD ocument 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(xslt FilePath);

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

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(s tr,Encoding.UTF 8);
trans.Transform (docToTransform ,null,xWriter,n ull);

//load the new doc from the stream
docToReturn.Loa d(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 6043


Graham Pengelly wrote:

I am trying to transform on System.Xml.XmlD ocument 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(xslt FilePath);

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

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(s tr,Encoding.UTF 8);
trans.Transform (docToTransform ,null,xWriter,n ull);

//load the new doc from the stream
docToReturn.Loa d(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.XPat h;
using System.Xml.Xsl;

public class Test20040716 {
public static void Main (string[] args) {
string xslURL = @"test20040716X sl.xml";
string xmlURL = @"test200407160 1.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(xmlURL);
XmlDocument resultDocument = (XmlDocument) Transform(xmlDo cument,
xslURL);
Console.WriteLi ne(resultDocume nt.OuterXml);
}

public static IXPathNavigable Transform (
IXPathNavigable input,
string stylesheetURL
)
{
XslTransform xsltProcessor = new XslTransform();
xsltProcessor.L oad(stylesheetU RL);
IXPathNavigable output = null;
if (input is XmlDocument) {
XmlDocument outputXML = new XmlDocument();
outputXML.Load( xsltProcessor.T ransform(input, (XsltArgumentLi st)
null, (XmlResolver) null));
output = outputXML;
}
else if (input is XPathDocument) {
output = new XPathDocument(x sltProcessor.Tr ansform(input,
(XsltArgumentLi st) 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.XmlD ocument 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(xslt FilePath);

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

//transform the original doc into a memorystream
Stream str = new MemoryStream();
XmlWriter xWriter = new XmlTextWriter(s tr,Encoding.UTF 8);
trans.Transform (docToTransform ,null,xWriter,n ull);

//load the new doc from the stream
docToReturn.Loa d(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.XPat h;
using System.Xml.Xsl;

public class Test20040716 {
public static void Main (string[] args) {
string xslURL = @"test20040716X sl.xml";
string xmlURL = @"test200407160 1.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(xmlURL);
XmlDocument resultDocument = (XmlDocument) Transform(xmlDo cument,
xslURL);
Console.WriteLi ne(resultDocume nt.OuterXml);
}

public static IXPathNavigable Transform (
IXPathNavigable input,
string stylesheetURL
)
{
XslTransform xsltProcessor = new XslTransform();
xsltProcessor.L oad(stylesheetU RL);
IXPathNavigable output = null;
if (input is XmlDocument) {
XmlDocument outputXML = new XmlDocument();
outputXML.Load( xsltProcessor.T ransform(input, (XsltArgumentLi st)
null, (XmlResolver) null));
output = outputXML;
}
else if (input is XPathDocument) {
output = new XPathDocument(x sltProcessor.Tr ansform(input,
(XsltArgumentLi st) 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
3622
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" indent="no" omit-xml-declaration="yes" standalone="no" /> some of my transforms fail with the following exception: System.Xml.Xsl.XsltException: There are multiple root
1
1892
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 by the following short C# program. The program throws an exception saying that "ac" is an invalid XPath expression. using System; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl;
1
1684
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 with MSXML, where I get
3
2269
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 use this so that we can specify different templates to be used by the same xml node. Know that we could use global params, but would rather not have to update all teh stylesheets and any code that uses msxml and the same stylesheets.
1
1101
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); } } public void Transform(XmlDocument xml,XmlDocument xsl){
2
3719
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) methods, and overrides for this task. I have verified that the source XML and XSL will transform by means of using the MSXML command line parsing tool (previous version of MSXML though). Here is my code:
0
273
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 and stylesheet, so you can assume (ahem) that they are valid. I am using 1.1 The code is below: Dim sre As System.IO.StringReader = New System.IO.StringReader(ResultsDataSet.GetXml())
1
2919
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; xslobj.load(xslpath); Here inserting an 'xsl:import' node to the xsl before doing transform. This xsl contains some common templates which can be used across all xsl's. So no need of doing import of this specifically in all xsl's.
9
2502
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 use XslCompiledTransform. But all this was rendered using an Xml control and I can't find a way to relate this control to an XslCompiledTransform ? Any help appreciated to solve this migration pb.
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2191
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.