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.
- Launch Visual Studio.NET 2005.
- Click on File menu, then select New and click Project.
- Select Visual C# Project in Project Types.
- Select ASP.NET Web Application in the Template box.
- Specify the Project's Location and click OK.
- Creating XSD schema into an XML file. Here is the sample schema code:
XML Copy 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>
- Below is the sample for XML data:
XML Copy 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>
- Drag WebGrid into WebForm.
- Double click the WebGrid and add the following codes under InitializeDataSource event handler:
C# Copy 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; }
- Then, it will automatically retrieve its structure by using RetrieveStructure() method in PrepareDataBinding event handler:
C# Copy Code if(!IsPostBack) { WebGrid1.RetrieveStructure(); }
- Compile and run the WebForm.