Hey Geeks,
When it comes to the .net framwork development, We have seen that .net languages can easily manipulate XML and take advantage of all the benifits provided by XML.
This article will support both .net 1.1 & .net 2.0, I have written the code in 2.0, but as we know that .net framework 2.0 is providing lots of new objects for XML manipulation. that are out of the scope of this article so let us get down to the business and show you the real code.
'New object of XmlDoc and file loading
Dim XMLDoc As New XmlDocument
XMLDoc.Load(Server.MapPath("MyData.xml"))
'Extracting the root node of Xml file (Item) in a collection called XmlNodeList
Dim Item As XmlNodeList = XMLDoc.GetElementsByTagName("Item")
'For data display
Dim dt As New DataTable
Dim dtColumn As DataColumn
Dim dtRow As DataRow
'New Column
dtColumn =
New DataColumn("ItemId")
dt.Columns.Add(dtColumn)
'New Column
dtColumn =
New DataColumn("ProductName")
dt.Columns.Add(dtColumn)
'New Column
dtColumn =
New DataColumn("Price")
dt.Columns.Add(dtColumn)
'This loop will examine all nodes find appropiate and fill the data row
Dim a As Integer
For a = 0 To Item.Count - 1
dtRow = dt.NewRow
Dim ItemNode As XmlNode = Item(a)
Dim child As XmlElement
For Each child In ItemNode.ChildNodes
Select Case child.Name.ToLower()
Case "itemid"
dtRow(
"ItemId") = child.InnerText
Case "productname"
dtRow(
"ProductName") = child.InnerText
Case "price"
dtRow(
"Price") = child.InnerText
End Select
Next
dt.Rows.Add(dtRow)
Next
GridView1.DataSource = dt
GridView1.DataBind()
Thats how we can get the result of our XML file in datatable without using dataset.