Share via


Xml Web Server Control Declarative Syntax 

Displays an XML document or the results of an XSL Transform.

<asp:Xml
    DocumentSource="uri"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    TransformSource="string"
    Visible="True|False"
/>

Remarks

Use the Xml control to display the contents of an XML document or the results of an XSL Transform.

The XML document to display is specified by setting one of three properties. These three properties represent the different types of XML documents that can be displayed. You can display a XmlDocument, an XML string, or an XML file by setting the appropriate property. The following table lists the properties for specifying the XML document.

Property Description

Document

Sets the XML document using a XmlDocument object.

DocumentContent

Sets the XML document using a string.

NoteNote

This property is commonly set declaratively by placing text between the opening and closing <asp:Xml> tags of the Xml control.

DocumentSource

Sets the XML document using a file.

NoteNote

At least one of the XML document properties must be set or no XML document is displayed. If more than one XML document property is set, the XML document in the last property set is displayed. The documents in the other properties are ignored.

You can optionally specify an XSL Transform document that formats the XML document before it is written to the output stream by setting one of two properties. The two properties represent the different types of XSL Transform documents that can be used to format the XML document. You can format the XML document with a XslTransform or with an XSL Transform file by setting the appropriate property. If no XSL Transform document is specified, the XML document is displayed using the default format. The following table lists the properties for specifying an XSL Transform document.

Property Description

Transform

Formats the XML document using the specified XslTransform.

TransformSource

Formats the XML document using the specified XSL Transform file.

NoteNote

The XSL Transform document is optional. You do not need to set the Transform or the TransformSource properties. If both XSL Transform document properties are set, the last property set determines which XSL Transform document is used to format the XML document. The other property is ignored.

The Xml class also provides the TransformArgumentList property, which allows you to provide the XSL Transform with optional arguments. The arguments can be either XSLT parameters or extension objects.

Example

The following shows a sample XML file.

<People>
   <Person>
      <Name>
         <FirstName>Joe</FirstName>
         <LastName>Suits</LastName>
      </Name>
      <Address>
         <Street>1800 Success Way</Street>
         <City>Redmond</City>
         <State>WA</State>
         <ZipCode>98052</ZipCode>
      </Address>
      <Job>
         <Title>CEO</Title>
         <Description>Wears the nice suit</Description>
      </Job>
   </Person>

   <Person>
      <Name>
         <FirstName>Linda</FirstName>
         <LastName>Sue</LastName>
      </Name>
      <Address>
         <Street>1302 American St.</Street>
         <City>Paso Robles</City>
         <State>CA</State>
         <ZipCode>93447</ZipCode>
      </Address>
      <Job>
         <Title>Attorney</Title>
         <Description>Stands up for justice</Description>
      </Job>
   </Person>

   <Person>
      <Name>
         <FirstName>Jeremy</FirstName>
         <LastName>Boards</LastName>
      </Name>
      <Address>
         <Street>34 Palm Avenue</Street>
         <City>Waikiki</City>
         <State>HI</State>
         <ZipCode>98052</ZipCode>
      </Address>
      <Job>
         <Title>Pro Surfer</Title>
         <Description>Rides the big waves</Description>
      </Job>
   </Person>

   <Person>
      <Name>
         <FirstName>Joan</FirstName>
         <LastName>Page</LastName>
      </Name>
      <Address>
         <Street>700 Webmaster Road</Street>
         <City>Redmond</City>
         <State>WA</State>
         <ZipCode>98073</ZipCode>
      </Address>
      <Job>
         <Title>Web Site Developer</Title>
         <Description>Writes the pretty pages</Description>
      </Job>
   </Person>
</People>

The following shows a sample XSL Transform file.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/People">
      <xsl:apply-templates select="Person" />
   </xsl:template>
  
   <xsl:template match="Person">
      <table width="100%" border="1">
         <tr>
            <td>
               <b>
                  <xsl:value-of select="Name/FirstName" />
                  &#160;
                  <xsl:value-of select="Name/LastName" />
               </b>
            </td>
         </tr>
         <tr>
            <td>
               <xsl:value-of select="Address/Street" /><br />
               <xsl:value-of select="Address/City" />
               , 
               <xsl:value-of select="Address/State" /> 
               <xsl:value-of select="Address/Zip" />
            </td>
         </tr>
         <tr>
            <td>
               Job Title: <xsl:value-of select="Job/Title" /><br />
               Description: <xsl:value-of select="Job/Description" />
            </td>
         </tr>
      </table>
   </xsl:template>

   <xsl:template match="bookstore">
      <!-- Prices and books -->
      <bookstore>
         <xsl:apply-templates select="book"/>
      </bookstore>
   </xsl:template>

   <xsl:template match="book">
      <book>
         <xsl:attribute name="ISBN">
            <xsl:value-of select="@ISBN"/>
         </xsl:attribute>
         <price>
            <xsl:value-of select="price"/>
         </price>
         <xsl:text>
         </xsl:text>
      </book>
   </xsl:template>

</xsl:stylesheet>

The following example demonstrates how to use the Xml control to display the sample XML file using the sample XSL Transform file. Be sure the sample XML file is called People.xml and the sample XSL Transform file is called Peopletable.xsl.

<html>
<body>
   <h3>Xml Example</h3>
   <form runat="server">
      <asp:Xml id="xml1" 
           DocumentSource="people.xml" 
           TransformSource="peopletable.xsl" 
           runat="server" />
   </form>
</body>
</html>

The following example demonstrates how to create XmlDocument and XslTransform objects from the sample XML and XSL Transform files. The objects are then used by the Xml control to display the XML document. Be sure the sample XML file is called People.xml and the sample XSL Transform file is called Peopletable.xsl.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
         Dim doc As XmlDocument = New XmlDocument()
         doc.Load(Server.MapPath("people.xml"))

         Dim trans As XslTransform = new XslTransform()
         trans.Load(Server.MapPath("peopletable.xsl"))

         xml1.Document = doc
         xml1.Transform = trans
      End Sub
</script>
<body>
   <h3>Xml Example</h3>
   <form runat="server">
      <asp:Xml id="xml1" runat="server" />
   </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
         XmlDocument doc = new XmlDocument();
         doc.Load(Server.MapPath("people.xml"));

         XslTransform trans = new XslTransform();
         trans.Load(Server.MapPath("peopletable.xsl"));

         xml1.Document = doc;
         xml1.Transform = trans;
      }
   </script>
<body>
   <h3>Xml Example</h3>
      <form runat="server">
         <asp:Xml id="xml1" runat="server" />
      </form>
</body>
</html>

See Also

Reference

Xml

Other Resources

Web Server Control Syntax