473,402 Members | 2,046 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,402 software developers and data experts.

how to apply xslt to XmlDocument

Can someone answer this probably obvious question.

I have an xmldocument that i want to apply an xslt file to.
All the examples i find assume u are applying the xslt to an xml file
you've read in.
How can i apply the xslt to an XmlDocument i've just built?

This is wrecking my head, so any code sampe would be greatly
appreciated !

thnak you
Nov 12 '05 #1
3 10950
See this:
http://msdn.microsoft.com/library/en...lTransform.asp.
Includes a working example in two of your favorite languages.

Save your head. Hint for next time: Google on "site:msdn.microsoft.com .net
xslt xmldocument". The top three items are all relevent.

Cheers,
Stuart Celarier, Fern Creek
Nov 12 '05 #2
Here's a little class to make things easier for you ...

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
namespace XmlUtils {
/// <summary>
/// Transformer performs an XSLT Transformation.
/// It can be re-used, and is (probably) thread-safe.
/// </summary>
public class XsltTransformer {
private XmlDocument m_objXMLDoc;
private XslTransform m_objXSLTransform;
public XsltTransformer() {
m_objXMLDoc =new XmlDocument();
m_objXSLTransform = new XslTransform();
}
// With an XmlDocument
public void setXML(XmlDocument input) {
m_objXMLDoc = input;
}
// With an input stream
public void setXML(Stream input) {
if(input!=null)
m_objXMLDoc.Load(input);
}
// With an XML reader
public void setXML(XmlReader reader) {
if(reader!=null)
m_objXMLDoc.Load(reader);
}
// With a plain string
public void setXML(string strXML) {
if(strXML.Length > 0)
m_objXMLDoc.LoadXml(strXML);
}
public string getXML() {
if(m_objXMLDoc!=null)
return m_objXMLDoc.InnerXml;
else
return null;
}

// With a plain stringpath
public void setXSL(string strXMLPath) {
if(strXMLPath.Length > 0)
m_objXSLTransform.Load(strXMLPath);
}
// With an XmlDocument instance
public void setXSL(XmlDocument xsl) {
m_objXSLTransform.Load(xsl.DocumentElement.CreateN avigator());
}
public void Transform(System.IO.TextWriter output) {
XPathNavigator navigator = m_objXMLDoc.DocumentElement.CreateNavigator();
XmlTextWriter writer = new XmlTextWriter(output);
m_objXSLTransform.Transform(navigator, null, writer);
}
public string TransformToString() {
XPathNavigator navigator = m_objXMLDoc.DocumentElement.CreateNavigator();
StringBuilder sb = new StringBuilder();
StringWriter swriter = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(swriter);
m_objXSLTransform.Transform(navigator, null, writer);
return sb.ToString();
}
}
}

Angel
O:]
Nov 12 '05 #3
TnaG wrote:
Can someone answer this probably obvious question.

I have an xmldocument that i want to apply an xslt file to.
All the examples i find assume u are applying the xslt to an xml file
you've read in.


What kind of example do you nedd? XslTransform's Transform() method
accepts XmlDocument as is, 'cause XmlDocument is IXPathNavigable.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

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

Similar topics

6
by: JJBean | last post by:
Hi Oleg and All, Is this correct? Can I do this? <code> XmlDocument doc=new XmlDocument(); XPathNavigator nav = doc.CreateNavigator(); //Load the xml file. Change path if needed...
0
by: marcum williams | last post by:
Below I'm generating an updategram via xslt and currently referencing schema via <updg:sync mapping-schema="hpXSD.xsd"> .... </updg:sync> But I would like to LOAD the .xsd schema into memory...
0
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag,...
8
by: Eric Phetteplace | last post by:
Hello, I am moving over from using msxml2.domdocument to system.xml and system.xml.xsl. I'd like to present this to my group today, so your help is appreciated. I want to transform an XML...
2
by: Wayne Wengert | last post by:
I have an xml file in which I reference an xsl template file to convert the xml to HTML. My system also has VSNET 2003 installed and when I open the xml file in IE6 it opens the file in VSNET...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
1
by: JJBean | last post by:
How do u apply a XSLT transform to an XmlDocument in c#? Could you please show me this in code ? Thanks, JJBean
1
by: cameron | last post by:
I am attempting to pass in an XmlDocument as a parameter to a transform: from my sample ASPX page: string BaseDir = "/cameron/Play/ComplexParam/"; XmlDocument XSL = new XmlDocument();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.