Published:
11 Nov 2001
|
Abstract
This part takes a look at the role that XML data plays in .NET and how we can use XML data as a datasource. |
|
by . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
17718/
23
|
|
|
Introduction |
ASP.NET & Databases : Part 5
Published 11/11/01
Introduction
In Parts 1 - 4 we have talked about connecting, storing, viewing and in this part we take a break and look at some XML. XML in .NET is a very important part, XML is used in nearly anything in .NET, from web services, to datasets. Even SQL Server 2000 supports XML. XML has taken a good foothold in data storage. I don't see it as a very good way to store my data (but that's my opinion). For this part we will be using the XML file here. I won't be covering all the data techniques that you can do in XML (writing, modifying, appending etc.) I'll be just covering its involvement in ADO.NET |
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.
|
More on XML |
More on XML
This was quite a short part on XML and there is a lot more on XML out there. I'm not that well educated in the field of XML (mainly because I don't plan on using it for anything that would need me to learn the intricate details of it). This parts was designed to show you one of the many roles of XML in ASP.NET. If you check out my munted calculator at http://authors.aspalliance.com/wisemonk/articles/samples/cal.asmx and try to add something, you will see that the result is an XML file.
If you want to learn more about XML then take a look at the official definition at http://www.w3.org/XML/ or get a less technical look at http://www.4guysfromrolla.com/webtech/xml.shtml. |
|
|
User Comments
No comments posted yet.
|
Product Spotlight
|
|