Export (0) Print
Expand All

Loading XML Data in the XML Web Server Control

Visual Studio .NET 2003

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 XML control to the Web Forms page. For details, see Adding XML Web Server Controls to a Web Forms Page.
  2. Set the DocumentSource property of the control to the path to the XML source document.
    Note   You need to be sure that when your application runs, it has sufficient permissions to read the XML file. For details, see Access Permissions for Web Applications.

    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 XML control to the Web Forms page. For details, see Adding XML Web Server Controls to a Web Forms Page.
  2. On the View menu, click Code. In the Code Editor, find the Page_Load procedure.
  3. Add code to load the XML source document, and assign the source to the Document property of the control. For example:
    ' Visual Basic
    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()
       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#
    private void Page_Load(object sender, System.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;
    }
    

To include the XML content inline

  1. Add an XML control to the Web Forms page. For details, see Adding XML Web Server Controls to a Web Forms Page.
  2. In HTML view, 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

Introduction to the XML Web Server Control | Adding XML Web Server Controls to a Web Forms Page | Transforming XML Data in the XML Web Server Control | Walkthrough: Displaying an XML Document in a Web Forms Page using Transformations

Show:
© 2014 Microsoft