Controls You Can Use on Web Forms ASP.NET Standard Controls Xml Control
An XSL transformations ( XSLT ) style sheet ( .xslt or .xsl file ) is used to transform the content of a source XML document into a presentation that is tailored specifically to a particular user, media, or client. There are two ways to transform XML data in the Xml Web server control:
- Point to an external XSLT file, which will automatically apply the transformation to the XML document.
- Apply a transformation that is an object of type XslTransform to the XML document.
Both methods have the same results, and your choice is dependent primarily on what is most convenient in your application. If the transformation is in the form of an .xsl or .xslt file, it is easy to load the file. If the transformation is in the form of an object — such as when it is being passed to your application by another process — then you can apply it as an object.
NOTE: The XslTransform class also allows you to load an .xsl or .xslt file into the instance of the transformation.
- Add an <
asp:Xml
> control into the Web Forms page. For syntax, see Xml Control Syntax.
- Set the XML control’s TransformSource property to the path to the XSLT document.
The following example shows how you can apply a transformation from a file to an XML control called Xml1.
Xml1.TransformSource = "myStyle.xsl";
Xml1.TransformSource = "myStyle.xsl" |
|
C# |
VB |
- Create an instance of the XslTransform class.
- Set the XML control’s Transform property to the instance of the transformation.
The following example shows how you can create an instance of the transformation class and use it to apply the transformation to an object. In this example, both the XML document and the transformation are read from files, but in a real application, both objects might come from another component. The transformation is applied as soon as the page loads.
void Page_Load ( object src, EventArgs e ) {
System.Xml.XmlDocument doc = new System.Xml.XmlDocument ( );
doc.Load ( Server.MapPath ( "mySource.xml" ) );
System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform ( );
trans.Load ( Server.MapPath ( "myStyle.xsl" ) );
Xml1.Document = doc;
Xml1.Transform = trans;
}
Sub Page_Load ( ByVal src As Object, ByVal e As EventArgs )
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument ( )
doc.Load ( Server.MapPath ( "mySource.xml" ) )
Dim trans As System.Xml.Xsl.XslTransform = New System.Xml.Xsl.XslTransform
trans.Load ( Server.MapPath ( "myStyle.xsl" ) )
Xml1.Document = doc
Xml1.Transform = trans
End Sub |
|
C# |
VB |
Adding Xml Controls to a Web Forms Page Loading XML Data in the Xml Control