asp.net.ph

Skip Navigation Links

Transforming XML Data in the Xml Control

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.

To apply a transformation from a file

  1. Add an <asp:Xml> control into the Web Forms page. For syntax, see Xml Control Syntax.
  2. 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";
  C# VB

To apply a transformation from an XslTransform object

  1. Create an instance of the XslTransform class.
  2. 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;
}
  C# VB

See Also

Adding Xml Controls to a Web Forms Page   Loading XML Data in the Xml Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note