Datastream Cowboys

Your cart is empty

C#/XML/dataset

Have you ever wanted to use XML data in a table like you can with SQL Databases? Have you found out that XML and C# information out there lacking at best? Well look no further.. First off let's say we have an XML file called locations.xml. In this xml file we have 2 "tables" the first would be like this:

<states>
   <SNick>TX</SNick>
   <SName>Texas</SName>
</states>
<states>
   <SNick>OK</SNick>
   <SName>Oklahoma</SName>
</states>

And the second "table" would look like this:

<counties>
   <cid>1</cid>
   <state>Texas</state>
   <CName>Anderson</CName>
   <CCity>Palestine</CCity>
   <CZip>75801</CZip>
</counties>
<counties>
   <cid>3</cid>
   <state>Texas</state>
   <CName>Angenlina</CName>
   <CCity>Lufkin</CCity>
   <CZip>75902</CZip>
</counties>

So.. Now that we have our xml file, how do we connect to it? It's actully very simple.
private void mnuOpen_Click(object sender, System.EventArgs e)
{
    DialogResult res=openFileDialog.ShowDialog(this);
    if (res==DialogResult.OK)
    {
        string fn=openFileDialog.FileName;
        DataSet ds=new DataSet();
        ds.ReadXml(fn);
        dataSet=ds;
    }
}


next we create a datagrid called dgData. and add the following code:
   dgData.SetDataBinding(dataSet, "counties");
   dgData.CaptionText = "Table: Counties";
   

Support Open Source