XML data
<XML version = "1.0"?>
<Citylist>
<City code = "01" name = "Beijing" countryID = "China" abbr = "Beijing"/>
<City code = "01" name = "Shanghai" countryID = "China" abbr = "Shanghai"/>
</Citylist>
</XML>
// Access the attributes of the XML node and generate table data
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (file name or textreader );
Xmlnodelist xnl = xmldoc. selectnodes ("/citylist/City"); // query XML nodes
// Create a able
Datatable dt = new datatable ();
If (xnl. Count> 0)
{
// Add a table title row
Xmlnode xn = xnl [0];
For (INT I = 0; I <XN. Attributes. Count; I ++)
{
DT. Columns. Add (New datacolumn (Xn. attributes [I]. Name, typeof (system. String )));
}
// Add Table data rows
For (Int J = 0; j <xnl. Count; j ++)
{
Datarow DR = DT. newrow ();
For (int K = 0; k <xnl [J]. Attributes. Count; k ++)
{
Dr [k] = xnl [J]. attributes [K]. value;
}
DT. Rows. Add (DR );
}
}
Else
{
Dt = NULL;
}