asp.net.ph

Skip Navigation Links

Loading XML Data in the XML COntrol

Controls You Can Use on Web Forms   ASP.NET Standard Controls   Xml Control


There are three ways to load XML data into the XML Web server control.

  • Provide a path to an external XML document, using the DocumentSource property.
  • Load an XML document as an object and pass it to the control, using the Load method in the Page_Load event and assigning the document to the Document property of the XML control.
  • Include the XML content inline, between the opening and closing tags of the control.

To provide a path to an external XML document

  1. Add an <asp:Xml> control into the Web Forms page. For syntax, see Xml Control Syntax.
  2. Set the DocumentSource property of the control to the path to the XML source document.

    The XML document will be written directly to the output stream unless you also specify the TransformSource property. TransformSource must be a valid XSL Transformations document, which will be used to transform the XML document before its contents are written to the output stream. The following example shows how to refer to source documents by using a relative path.

    <body>
       <h3>XML Example</h3>
       <form runat=server>
          <asp:Xml id="xml1" DocumentSource="MySource.xml"
             TransformSource="MyStyle.xsl" runat="server" />
        </form>
    </body>

To load an XML document as an object and pass it to the control

  1. Add an <asp:Xml> control into the Web Forms page. For syntax, see Xml Control Syntax.
  2. Add code to load the XML source document, and assign the source to the Document property of the control. For example:
    private 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

To include the XML content inline

  1. Add an <asp:Xml> control into the Web Forms page. For syntax, see Xml Control Syntax.
  2. Find the <asp:Xml> and </asp:Xml> tags.
  3. Add your XML code between these two tags. For example:
    <asp:xml TransformSource="MyStyle.xsl" runat=server>
        <clients>
            <name>Frank Miller</name>
            <name>Judy Lew</name>
        </clients>
    </asp:xml>
See Also

Adding Xml Controls to a Web Forms Page   Transforming 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