471,594 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 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 5881


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rodger McNab | last post: by
1 post views Thread by Geir Sørensen | last post: by
1 post views Thread by fawzib.rojas | last post: by
reply views Thread by Ruprict | last post: by
1 post views Thread by Praveen | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | 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.