简体   繁体   中英

how to apply XSL to XML with xsltargument using c#?

i have a user control in which i want to pass page url to my XSL sheet and apply it to my XML file . as a result of it should produce some HTML output. Please explain how to do this?

xml file name: myXml.xml 
Xsl file name: mwXsl.xsl
Parameter to be passed by usercontrol(.cs) file: url of parent page.

i've defined XSL stylesheet. All i need to pass the argument to that file. in usercontrol(ascx) file i have this code

<asp:Xml ID="BControl" runat="server" DocumentSource="/wsitemap.sitemap" TransformSource="/Bread.xslt"></asp:Xml>

where wsitemap.sitemap is a XML file. if my XSLT get the argument the above code will be able to produce the desired output. so i need c# code for that.

in usercontrol (.cs file) i have :

public static string Transform(string xml, string xsl, XsltArgumentList argsList) 
    { 
        XDocument selectedXml = XDocument.Parse(xml);
        XslCompiledTransform xmlTransform = new XslCompiledTransform();
        StringBuilder htmlOutput = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(htmlOutput);
        xmlTransform.Load(new XmlTextReader(new StringReader(xsl)));
        xmlTransform.Transform(selectedXml.CreateReader(), argsList, writer);
        return htmlOutput.ToString();
    } 
    protected void Page_Load(object sender, EventArgs e)
    {
        string curPageId = Page.Request.Url.AbsoluteUri;
        XsltArgumentList xslArgs = new XsltArgumentList();
        xslArgs.AddParam("curPage", "", curPageId);
        string output=Transform("wsitemap.sitemap","bread.xslt",xslArgs);
        Response.Clear(); Response.Write(output);
    }

but this code is not working , any other approach plz tell me

You have use , XsltArgumentList C# class to pass arguments. You can add all your parameters there and pass to the xsl.

Please look into following SO Link,

Passing parameters to XSLT Stylesheet via .NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM