A Quick Wilson.XmlDbClient Sample
Dennis was pointing out that the documentation file that I threw together for WilsonXmlDbClient didn't include any sample code to give beginners a little head start. I had intended to do more work on the documentation but lost track of it.
For those who don't know, WilsonXmlDbClient is an excellent open source ADO.NET XML Provider by Paul Wilson that lets you treat XML like SQL data.
Anyway, here's some quick Windows Forms/VB/C# code and a sample XML file in case anyone needs it. The sample XML data file is shown afterwards. Just throw a datagrid on the form and hook it up!
For more info, be sure to check out Paul Wilson's blog.
Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim xmldbconn As New Wilson.XmlDbClient.XmlDbConnection
Dim strBasePath = Environment.CurrentDirectory & "\..\"
xmldbconn.ConnectionString = strBasePath & _
"Average_tuition_fees_by_faculty.xml"
Dim xmldbcmd As New Wilson.XmlDbClient.XmlDbCommand
xmldbcmd.Connection = xmldbconn
xmldbcmd.CommandText = "SELECT Name, Fee2003_04, " & _
"Fee2004_05 from Faculty Where Fee2004_05 > 4000 ORDER BY Fee2004_05"
Dim xmldbda As Wilson.XmlDbClient.XmlDbDataAdapter
xmldbda = New Wilson.XmlDbClient.XmlDbDataAdapter(xmldbcmd)
Dim ds As New DataSet
xmldbda.Fill(ds)
DataGrid1.DataSource = ds.Tables(0)
End Sub
private void Form1_Load(object sender, System.EventArgs e)
{
Wilson.XmlDbClient.XmlDbConnection xmldbconn = new Wilson.XmlDbClient.XmlDbConnection();
string strPath = Environment.CurrentDirectory + @"\..\..\";
xmldbconn.ConnectionString = strPath + "Average_tuition_fees_by_faculty.xml";
Wilson.XmlDbClient.XmlDbCommand xmldbcmd = new Wilson.XmlDbClient.XmlDbCommand();
xmldbcmd.Connection = xmldbconn;
xmldbcmd.CommandText = "SELECT Name, Fee2003_04, Fee2004_05 from Faculty Where Fee2004_05 > 4000 ORDER BY Fee2004_05";
Wilson.XmlDbClient.XmlDbDataAdapter da = new Wilson.XmlDbClient.XmlDbDataAdapter(xmldbcmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Tuitions desc="Average tuition fees by faculty">
<xs:schema id="data" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="data" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Faculty">
<xs:complexType>
<xs:sequence>
<xs:element name="id" msdata:AutoIncrement="true" type="xs:decimal" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Fee2003_04" type="xs:double" minOccurs="0" />
<xs:element name="Fee2004_05" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Faculty>
<Name>Agriculture</Name>
<Fee2003_04>3495</Fee2003_04>
<Fee2004_05>3626</Fee2004_05>
</Faculty>
<Faculty>
<Name>Architecture</Name>
<Fee2003_04>3587</Fee2003_04>
<Fee2004_05>3610</Fee2004_05>
</Faculty>
<Faculty>
<Name>Arts</Name>
<Fee2003_04>3813</Fee2003_04>
<Fee2004_05>3935</Fee2004_05>
</Faculty>
<Faculty>
<Name>Commerce</Name>
<Fee2003_04>3985</Fee2003_04>
<Fee2004_05>4118</Fee2004_05>
</Faculty>
<Faculty>
<Name>Dentistry</Name>
<Fee2003_04>11681</Fee2003_04>
<Fee2004_05>12331</Fee2004_05>
</Faculty>
<Faculty>
<Name>Education</Name>
<Fee2003_04>3149</Fee2003_04>
<Fee2004_05>3240</Fee2004_05>
</Faculty>
<Faculty>
<Name>Engineering</Name>
<Fee2003_04>4400</Fee2003_04>
<Fee2004_05>4617</Fee2004_05>
</Faculty>
<Faculty>
<Name>Household sciences</Name>
<Fee2003_04>3669</Fee2003_04>
<Fee2004_05>3878</Fee2004_05>
</Faculty>
<Faculty>
<Name>Law</Name>
<Fee2003_04>5995</Fee2003_04>
<Fee2004_05>6471</Fee2004_05>
</Faculty>
<Faculty>
<Name>Medicine</Name>
<Fee2003_04>9137</Fee2003_04>
<Fee2004_05>9977</Fee2004_05>
</Faculty>
<Faculty>
<Name>Music</Name>
<Fee2003_04>3759</Fee2003_04>
<Fee2004_05>3883</Fee2004_05>
</Faculty>
<Faculty>
<Name>Science</Name>
<Fee2003_04>3957</Fee2003_04>
<Fee2004_05>4094</Fee2004_05>
</Faculty>
<Faculty>
<Name>Undergraduate</Name>
<Fee2003_04>4018</Fee2003_04>
<Fee2004_05>4172</Fee2004_05>
</Faculty>
<Faculty>
<Name>Graduate</Name>
<Fee2003_04>5247</Fee2003_04>
<Fee2004_05>5475</Fee2004_05>
</Faculty>
</Tuitions>
Ken
Microsoft MVP [ASP.NET]