469,282 Members | 1,917 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by JJBean | last post: by
reply views Thread by marcum williams | last post: by
reply views Thread by Christopher M. Lauer | last post: by
8 posts views Thread by Eric Phetteplace | last post: by
2 posts views Thread by Wayne Wengert | last post: by
7 posts views Thread by Harolds | last post: by
1 post views Thread by JJBean | last post: by
1 post views Thread by cameron | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.