locked
render the results of the XML transform from the code-behind page in the body of my aspx page RRS feed

  • Question

  • User232401783 posted

    I started out in the WebMatrix forum with this post http://forums.asp.net/p/1978113/5664292.aspx?Re+Transform+XML+to+XHTML

    Mike pointed me to this article http://www.cmsproducer.com/analysis/XML-XSLT-XHTML-Transformation-PHP-ASP-NET

    I couldn't figure out how to emulate the ASP.NET code in WebMatrix, so in Visual Web Developer 2010 Express I created a C# aspx page from the "Web Form" template.  It created a code-behind page.  In that code-behind page I added

    using System.Xml;
    using System.Xml.Xsl;

    and in the Page_Load event I added the following code:

    XsltSettings settings = new XsltSettings(false,false);
    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load("path to the xsl file",settings, new XmlUrlResolver());

    trans.Transform("path to the xml document","destination file path");

    The code works, creating a destination file, but what I really want to do is render the results of a transform in a browser on the fly, presumably by passing the transform from the code-behind page to the body of the aspx page.  I know the Transform method is overloaded, but I'm not sure which version to use.  How do I render the results on the fly? xmlwriter? Some stream?

    I'm not very ASP.NET savvy, so bear with me.

    Thanks in advance for any assistance.

    Christian Bahnsen

    Tuesday, April 1, 2014 12:41 PM

Answers

  • User232401783 posted

    Update: I got my webmatrix/razor page working.  Here's the code I used:

    @using System.Xml;
    @using System.Xml.Xsl;
     
    @{
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load("C:\\Users\\Christian\\Documents\\data\\xml\\boitnott_xsl.xsl");
     
        string XHTMLoutput;
        StringWriter writer = new StringWriter();
        xslt.Transform("C:\\chris\\Boitnott_Mary.xml", null, writer);
        XHTMLoutput = writer.ToString();
        writer.Close();
    }
     
    @Html.Raw(XHTMLoutput)
    

    Those of you who use code-behind can probably figure out how to refactor it for your purposes.

    Thanks for the suggestions

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:47 AM

All replies

  • User1918509225 posted

    Hi ChristianBahnsen,

    About your post ,I create a demo for you.

    In my project ,I create one aspxpage ,one xslt file and one xml file.

    Here is my xslt file:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="list | store">
            <list>
                <xsl:for-each select="item | product | product-store">
                <item>
                    <name>
                        <xsl:choose>
                            <xsl:when test="name"><xsl:value-of select="substring-before(name, ' ')" /></xsl:when>
                            <xsl:otherwise><xsl:value-of select="name | title" /></xsl:otherwise>
                        </xsl:choose>
                    </name>
                    <desc>
                        <xsl:choose>
                            <xsl:when test="name"><xsl:value-of select="substring-after(name, ' ')" /></xsl:when>
                            <xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise>
                        </xsl:choose>
                    </desc>
                    <nr><xsl:value-of select="index | number" /></nr>
                </item>
                </xsl:for-each>
            </list>
        </xsl:template>
    </xsl:stylesheet>

    Here is my xml file:

    <?xml-stylesheet type="text/xsl" href="transform.xsl"?>
    <list>
      <item>
        <index>1362242627</index>
        <name>test 22</name>
      </item>
      <item>
        <index>2362625609</index>
        <name>test 4</name>
      </item>
      <item>
        <index>736274650</index>
        <name>test 76</name>
      </item>
    </list>

    Here is my aspx server code:

                XsltSettings settings = new XsltSettings(false, false);
                XslCompiledTransform trans = new XslCompiledTransform();
                trans.Load(Server.MapPath("XSLTFile1.xslt"), settings, new XmlUrlResolver());
    
                trans.Transform(Server.MapPath("XMLFile1.xml"), Server.MapPath("test.xml"));
                string xmlPath = MapPath("test.xml");
                XmlDocument xd = new XmlDocument();
                xd.PreserveWhitespace = true;
                Response.ContentType = "text/xml";
                xd.Load(xmlPath);
                Response.Write(xd.InnerXml);
                Response.End();

    Hope my answer will be helpful for you.

    Best Regrads,

    Kevin Shen.

    Wednesday, April 2, 2014 2:59 AM
  • User1777983149 posted

    public string xmlfile = "";
    public string xslfile = "";
    public string xslxmlfile = "";
    public string xslxmltxt = "";

    protected void Page_Load(object sender, EventArgs e)
    {
    string myXmlFile = Server.MapPath(xmlfile);
    XPathDocument myXPathDoc = new XPathDocument(myXmlFile);
    XslCompiledTransform myXslTrans = new XslCompiledTransform();
    string myStyleSheet = Server.MapPath(xslfile);
    XsltSettings xs = new XsltSettings(true, true);
    XmlUrlResolver xur = new XmlUrlResolver();
    myXslTrans.Load(myStyleSheet, xs, xur);

    myXslTrans.Transform(myXmlFile, Server.MapPath(xslxmlfile));
    string strHTML = GetHTML(Server.MapPath(xslxmlfile)); ;
    strHTML = strHTML.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
    divHTML.InnerHtml = strHTML;


    }
    public string GetHTML(string strURL)
    {
    string strResult = string.Empty;
    try
    {
    WebResponse objResponse;
    WebRequest objRequest = HttpWebRequest.Create(strURL);
    objResponse = objRequest.GetResponse();
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
    strResult = sr.ReadToEnd();
    sr.Close();
    }
    }
    catch (Exception ex)
    {
    // Response.Write(ex.ToString());
    }
    return strResult;
    }

    Tuesday, April 8, 2014 9:10 AM
  • User232401783 posted

    Thanks for your suggestions.

    I've actually got a webmatrix page coming very close to the mark.  Here's the link to my thread in the webmatrix forum: http://forums.asp.net/t/1978113.aspx?Transform+XML+to+XHTML

    As I mention in that post, this project is both easier and more difficult than I expected.  Simple, in that the Transform generates all the html code I need.  Difficult, in getting the code to stream to and render in a browser as a page.

    I think I'm going to pursue the webmatrix approach; I like the single-page approach.  The code-behind approach is harder for me to tie together.

    Thanks again

    Tuesday, April 8, 2014 12:15 PM
  • User232401783 posted

    Update: I got my webmatrix/razor page working.  Here's the code I used:

    @using System.Xml;
    @using System.Xml.Xsl;
     
    @{
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load("C:\\Users\\Christian\\Documents\\data\\xml\\boitnott_xsl.xsl");
     
        string XHTMLoutput;
        StringWriter writer = new StringWriter();
        xslt.Transform("C:\\chris\\Boitnott_Mary.xml", null, writer);
        XHTMLoutput = writer.ToString();
        writer.Close();
    }
     
    @Html.Raw(XHTMLoutput)
    

    Those of you who use code-behind can probably figure out how to refactor it for your purposes.

    Thanks for the suggestions

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 10:47 AM