ASP.NET & Databases Part 5
page 2 of 3
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17691/ 56

The XMLDataDocument

The XMLDataDocument

There are many ways to read XML, through the XMLTextReader and the XMLDataDocument. The XMLDataDocument thinks of XML as data like it would be in a DataSet. For those of you with some knowledge of how XML works in .NET, the XMLDataDocument is like the XMLDocument and can be used anywhere it can be, but the DataDocument automatically creates a DataSet and had more of the DataSet functions (relationships etc.). Lets look at an example of it in use. Remember to Import System.XML, System.Data and System.Data.OleDb

<script runat="server" language="VB">
sub page_load(sender as object, e as eventargs)
dim xdoc as new XMLDataDocument()

xdoc.DataSet.ReadXML(Server.MapPath("books.xml"))

dg1.DataSource = xdoc.DataSet
dg1.Datamember = xdoc.DataSet.Tables(0).TableName

dg1.DataBind()
end sub
</script>

<asp:datagrid id="dg1" runat="server" BorderColor="black" gridlines="vertical" font-name="arial" font-size="10pt" />

If you look through this code you will see that the xdoc.DataSet is just like any other DataSet, and we have put it into a DataGrid. However, if you look at the datagrid, you can see that there are fields like the author(s) missing. This is because the XML document has been divided up into tables depending on these rules -

  • Any elements with attributes become tables.
  • Any elements that contain other elements become tables.
  • If there are two or more elements with the dame name, they become a table.
  • All direct children of the root node become tables.
<script runat="server" language="VB">
sub page_load(sender as object, e as eventargs)
dim xdoc as new XMLDataDocument()

xdoc.DataSet.ReadXML(Server.MapPath("books.xml"))

dg1.DataSource = xdoc.DataSet
dg1.Datamember = xdoc.DataSet.Tables(0).TableName

dg1.DataBind()

dg2.DataSource = xdoc.DataSet
dg2.Datamember = xdoc.DataSet.Tables(1).TableName

dg2.DataBind()

end sub
</script>

<asp:datagrid id="dg1" runat="server" BorderColor="black" gridlines="vertical" font-name="arial" font-size="10pt" />
<asp:datagrid id="dg2" runat="server" BorderColor="black" gridlines="vertical" font-name="arial" font-size="10pt" />

As you can see in this table, we have taken the two tables that it has created and put them into datagrids. You can see that it has generated a book_Id column that identifies each <book> element which is directly under the root <books>. Each other element or attribute is assigned the ID of the <book> element's id that they are under, you can see that authors can have the same book_Id and can be duplicated for different books.

This is a powerful feature, you can do anything that you can do in a normal dataset now and can use XML data more effectively.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 12:05:27 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search