How to: Load XML Data in the XML Web Server 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 XmlDocument object and pass it to the control, using the Load method 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.


The Document property is now obsolete. For more information, see the XPathNavigator property.

To provide a path to an external XML document

  1. Add an Xml control to the Web Forms page.

  2. Set the DocumentSource property of the control to the path to the XML source document.


    You need to be sure that when your application runs, it has sufficient permissions to read the XML file.

    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 code example shows how to refer to source documents by using a relative path.

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

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

  1. Add an Xml control to the Web Forms page.

  2. Add code to load the XML source document, and assign the source to the Document property of the control. For example:

    Private Sub Page_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
       Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
       Dim trans As System.Xml.Xsl.XslTransform = _
          New System.Xml.Xsl.XslTransform
       Xml1.Document = doc
       Xml1.Transform = trans
    End Sub
    private void Page_Load(object sender, System.EventArgs e)
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        System.Xml.Xsl.XslTransform trans = new 
        Xml1.Document = doc;
        Xml1.Transform = trans;

To include the XML content inline

  1. Add an Xml control to the Web Forms page.

  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>
            <name>Frank Miller</name>
            <name>Judy Lew</name>

See Also


XML Web Server Control Overview