How to format and output XML documents using XSLT in Silverlight

Source: Internet
Author: User
Tags argumentlist xsl xslt

In. net, using javascompliedtransform allows you to easily format XML documents using XSLT,

 //  Load XML document Xpathdocument xmlxpathdoc = New Xpathdocument (httpcontext. Current. server. mappath ( "  Sample. xml  "  );  //  Load XSLT document Extends compiledtransform implements transform = New  Extends compiledtransform ();  // Add parameters to XSLT document (optional) Repeated targumentlist parameter Arg = New  Using targumentlist (); Using Arg. addparam (  "  Param1  " , String . Empty, "  Value1  "  ); Parameter Arg. addparam (  "  Param2  " ,String . Empty, "  Value2  "  );  //  Output Textwriter writer = New  Stringwriter (); transform. Transform (xmlxpathdoc, xslarg, writer); response. Write (writer. tostring ()); 

However, in Silverlight, the following two methods cannot be used:

1. Compile compiledtransformCodeMove to Web Service and call Web Service asynchronously in Silverlight.

2. Use a browser for parsing.

the first method is simple to implement. Net code in web service and then called in Silverlight. However, this method has some limitations, such as the need for additional deployment, access security, and permissions on the server. The second method is to use ActiveX in the browser instead of compile compiledtransform to parse the XML document. The code is completed by JavaScript and then called in Silverlight. The disadvantage is that you need to determine the browser type and then parse it in different ways. The following code describes how to call ActiveX to parse XML documents in IE browser and use it in Silverlight.

 htmlpage. window. eval (@  "  function transform (XML, XSL)  {  var  xmldom =  New  activexobject ("   msxml2.domdocument. 6.0   " ); xmldom. loadxml (XML);   var  against dom =  New  activeobject ("   msxml2.domdocument. 6.0   " ); Specify Dom. loadxml (XSL);   return   xmldom. transformnode (writable DOM) ;}  " );  

When using Silverlight, you only need to pass the content of the XML and XSLT documents in the string form,

 
StringResult = htmlpage. Window. Invoke ("Transform", Xmlstring, comment string)As String;

How do I pass parameters to the XSLT document? Let's take a look at the following code and encapsulate this function into a class.

 Public   Class  Effecttransformproxy {  Private   Const   String Script = "  Function Transform (XML, XSL) " + "  {{  " + "  VaR xmldoc = new activexobject (\ "msxml2.domdocument. 6.0 \");  " + "  Xmldoc. async = false;  " + "  Xmldoc. loadxml (XML );  " + "  VaR synchronized Doc = new activexobject (\ "msxml2.freethreadeddomdocument. 6.0 \");  " + "  Optional Doc. async = false;  " + "  Using Doc. loadxml (XSL );  " + "  VaR XSLT = new activexobject (\ "msxml2.20.template. 6.0 \"); " + "  XSLT. stylesheet = javasdoc;  " + "  VaR export proc = XSLT. createprocessor ();  " + "  Export Proc. Input = xmldoc;  " + "  {0}  " + "  Using Proc. Transform ();  " + "  Return response Proc. output;  " + "  }}  "  ;  Private List <parameter> Xslargumentlist;  Public Xelement upload document {Get ; Set  ;}  Public Xelement xmldocument { Get ; Set  ;}  Public   Void Addparam ( String Name, String  Param ){  If (Required argumentlist = Null ) {Javasargumentlist = New List <parameter> ();} Finally argumentlist. Add (  New  Parameter parameter (name, Param ));}  Public  Xelement transform () {stringbuilder scriptbuilder = New  Stringbuilder ();  If ( This . Xslargumentlist! = Null &&This . Repeated argumentlist. Count> 0  ){  Foreach (Parameter Param In   This  . Repeated argumentlist) {scriptbuilder. append (  "  Using Proc. addparameter (\"  " + Param. Name + "  \",\"  " + Param. Parameter + " \");  "  ) ;}} Htmlpage. Window. eval (  String  . Format (script, scriptbuilder. tostring ()));  Return Xelement. parse (htmlpage. Window. Invoke ( "  Transform  " , Xmldocument. tostring (), jsondocument. tostring ()) As   String  );}}  Public   Class Parameter parameter {  Private   String  Name;  Public   String  Name {  Get { Return  Name ;}  Set {Name = Value ;}}  Private   String  Parameter; Public   String  Parameter {  Get { Return  Parameter ;}  Set {Parameter = Value ;}}  Public  Parameter parameter (){}  Public Parameter parameter ( String _ Name, String  _ Parameter ){ This . Name = _ Name;  This . Parameter = _ Parameter ;}} 

Let's take a look at how to call:

Xelement = New Xelement ( "  Items  "  ); Xelement. Add (  New Xelement ( "  Item  " , " AA  " , New Xattribute ( "  ID  " , "  0  "  ); Xelement. Add (  New Xelement ( "  Item  " , "  Bb " , New Xattribute ( "  ID  " , "  1  "  ); Implements ttransformproxy transformproxy = New  Effecttransformproxy (); transformproxy. xmldocument = Xelement; transformproxy. addparam (  "  Param1 "  , "P1"); transformproxy. addparam (  "  Param2  "  , "P2"); transformproxy. invalid document = Xelement. Load ( New Uri ( "  Sample. XSLT  "  , Urikind. Relative). tostring (); xmldocument = Transformproxy. Transform ();

Of course, the JavaScript code injected to the page needs to be optimized to support multiple browsers. This method makes up for the inability to parse XML documents using the compiledtransform and xslttransform objects in Silverlight.

Reference document: http://msdn.microsoft.com/en-us/library/windows/desktop/ms763679 (V = vs.85). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.