Intersoft WebGrid Documentation
Walkthrough: Binding WebGrid with a XSD schema and a XML file data
See Also Send comments on this topic.

Glossary Item Box

This walkthrough shows you how to bind a XSD schema and a XML file data to WebGrid.

During this walkthrough, you will learn how to do the following:

  • Creating XSD schema into an XML file.
  • Read schema and load data from the XML file.
  • Use WebGrid's InitializeDataSource event to bind the DataSet.
  • Use WebGrid's PrepareDataBinding event to retrieve structure in WebGrid.

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to the XML database.
  • Visual Studio 2005 Application.

 Step-By-Step Instructions

To Bind WebGrid with a XSD schema and XML file data.

  1. Launch Visual Studio.NET 2005.
  2. Click on File menu, then select New and click Project.
  3. Select Visual C# Project in Project Types.
  4. Select ASP.NET Web Application in the Template box.
  5. Specify the Project's Location and click OK.
  6. Creating XSD schema into an XML file. Here is the sample schema code:

    XML Copy ImageCopy Code
    <ltxs:schema id="dsCiti" targetNamespace="http://www.tempuri.org/dsCiti.xsd" xmlns:mstns="http://www.tempuri.org/dsCiti.xsd"
            xmlns="http://www.tempuri.org/dsCiti.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
            attributeFormDefault="qualified" elementFormDefault="qualified">
            <ltxs:element name="dsCiti" msdata:IsDataSet="true">
                    <ltxs:complexType>
                            <ltxs:choice maxOccurs="unbounded">
                                    <ltxs:element name="Total">
                                            <ltxs:complexType>
                                                    <ltxs:sequence>
                                                            <ltxs:element name="ProfCode" type="xs:string" />
                                                            <ltxs:element name="ProfName" type="xs:string" minOccurs="0" />
                                                    </xs:sequence>
                                            </xs:complexType>
                                   </xs:element>
                                   </xs:choice>
                   </xs:complexType>
                <ltxs:unique name="Constraint1" msdata:PrimaryKey="true">
                        <ltxs:selector xpath=".//mstns:Level1" />
                        <ltxs:field xpath="mstns:Col1" />
                </xs:unique>
                <ltxs:keyref name="TotalLevel1" refer="mstns:Total_Constraint1">
                        <ltxs:selector xpath=".//mstns:Level1" />
                        <ltxs:field xpath="mstns:ProfCode" />
                </xs:keyref>
                <ltxs:key name="dsCitiKey1">
                        <ltxs:selector xpath=".//mstns:Level1" />
                        <ltxs:field xpath="mstns:Col1" />
                        <ltxs:field xpath="mstns:ProfCode" />
                </xs:key>
          </xs:element>
    </xs:schema>
    
  7. Below is the sample for XML data:

    XML Copy ImageCopy Code
    <?xml version="1.0" standalone="yes"?>
    <ltdsCiti xmlns="http://www.tempuri.org/dsCiti.xsd">
      <ltTotal>
        <ltProfCode<gt23</Profcode>
        <ltProfName<gtTestProfile</ProfName>
        <ltDV01_1M<gt879335.26</DV01_1M>
      </Total>
    </dsCity>
    
  8. Drag WebGrid into WebForm.
  9. Double click the WebGrid and add the following codes under InitializeDataSource event handler:

    C# Copy ImageCopy Code
    XmlDataDocument xDoc;
    private void WebGrid1_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
    {
        xDoc = new XmlDataDocument();
        xDoc.DataSet.ReadXmlSchema(Server.MapPath("dsCiti.xml"));
        xDoc.Load(Server.MapPath("MyXML.xml"));
        e.DataSource = xDoc.DataSet;
    }
    
  10. Then, it will automatically retrieve its structure by using RetrieveStructure() method in PrepareDataBinding event handler:

    C# Copy ImageCopy Code
    if(!IsPostBack)
    {
      WebGrid1.RetrieveStructure();
    }                              
    
  11. Compile and run the WebForm.

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.