Forum Moderators: open
I have been using XslTranform to load in an XML doc and tranform it using XSL. It all worked splendidly but little green lines under the code in Visual Studio suggested upgrading to XslCompiledTransform. I'm new to ASP.NET so it made sense to use the most current standards, but I'm begining to wish I hadn't bothered.
I can get the XslCompiledTransform to work - but it doesn't output the results into the asp:xml object like I want it to.
Working code using XslTranform:
In LeftMenuNav.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LeftMenuNav.ascx.cs" Inherits="managed_content_usercontrols_LeftMenu" %>
<asp:Xml ID="SubNavMenu" runat="server"></asp:Xml>
In LeftMenuNav.ascx.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
public partial class managed_content_usercontrols_LeftMenu : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument siteXML = new XmlDocument();
siteXML.Load(Server.MapPath("~/xml/dotnettemplate.sitemap"));
//
XslTransform sectionXSL = new XslTransform();
sectionXSL.Load(Server.MapPath("~/xslt/sectionmenu.xslt"));
//
SubNavMenu.Document = siteXML;
SubNavMenu.Transform = sectionXSL;
}
}
Working(ish) code using XslCompiledTransform:
LeftMenuNav.ascx as above
In LeftMenuNav.ascx.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
public partial class managed_content_usercontrols_LeftMenu : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// load site xml
XmlTextReader siteXML = new XmlTextReader(Server.MapPath("~/xml/dotnettemplate.sitemap"));
XPathDocument xpathDoc = new XPathDocument(siteXML);
//Section menu
XmlTextReader sectionXSL = new XmlTextReader(Server.MapPath("~/xslt/sectionmenu.xslt"));
XslCompiledTransform SubNavMenu = new XslCompiledTransform();
//
SubNavMenu.Load(sectionXSL);
//
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
//
SubNavMenu.Transform(xpathDoc, null, Response.Output);
}
}
At the moment this generates the transformed XML fine - but not to where the <asp:xml/> is, but instead to the top of the page. I know that this is because of the Response.Output but how do I save the results to the <asp:xml/> object?
I'm pretty sure it's something simple, but all the examples in google seem to be fine with outputting the data in the code, rather than to an object.
All help gratefully received!
try the following:
replace
<asp:Xml ID="SubNavMenu" runat="server"></asp:Xml>
with
<asp:Literal ID="litSubNavMenu" runat="server" />
SubNavMenu.Transform(xpathDoc, null, sw);
litSubNavMenu.Text = sw.ToString();
(I have no experience with the <asp.xml> control, so I've used a literal instead, but hopefully you get the general idea.)
It seems odd that the newer, shinier code in .NET 2.0 is longer and more complicated than the old 1.0 XslTransform method, but using the literal doesn't generate any errors so I think I'll use it for now until I have time to work out what the <asp:xml/> tag is supposed to actually do...
Thanks again.
It seems odd that the newer, shinier code in .NET 2.0 is longer and more complicated...
You can compact the code considerably by removing the XmlTextReaders and StringBuilder
XPathDocument xpathDoc = new XPathDocument(Server.MapPath("~/xml/dotnettemplate.sitemap"));
//Section menu
XslCompiledTransform SubNavMenu = new XslCompiledTransform();
SubNavMenu.Load(Server.MapPath("~/xslt/sectionmenu.xslt"));
//
using (StringWriter sw = new StringWriter())
{
SubNavMenu.Transform(xpathDoc, null, sw);
litSubNavMenu.Text = sw.ToString();
}
(I haven't tested this, but it should still work... I hope :))
And also here I would recommend caching either the documents or the output (preferably the output).