Work With Xml Data Sources

ASP.NET makes it easy for you to work with XML Data Sources. XML is a W3C (www.w3c.org) specified standard that is well accepted in the software development industry for describing data in text files. There are many companies that are adapting XML as a standard for transporting lightweight data. XML has become instrumental in having disparate systems to have a way to communicate to each other.

Sometimes you may wish to keep certain data in XML files on your Web server. XML is a very convenient way to store and transport data in your applications. You store XML documents in standard, nonbinary text files. This makes it easy to work with the documents. You can use any text-based viewer to inspect your XML documents.

To work with the XML file, you must first work with the FileStream object to open the file. Next, you need a StreamReader object for reading the byte stream from the FileStream object. The DataSet object has a ReadXML method that you can use to read the stream. After it has been read in, you can use a DataView based on the DataSet. Finally, the DataView can be bound to a DataGrid.

WORK WITH XML DATA SOURCES

Untitled - Notepad

File Edit Format Help

WORK WITH XML DATA SOURCES

Untitled - Notepad

File Edit Format Help

=;H3-Welcome to mylifetimegoals conWH3' Here are the goal categories and goal catego <P/>

CASP DATAGRID ID="datagridTitles" RUNA"

D Open

GenericTemplate.aspx from the Code Templates directory.

> Add a DataGrid control to the page.

^0 Import the System.IO and the System.Data namespaces.

JsJjiJ

3Import Namespace-'System.lO" %> 'y Import Namespace-'System.Data" %> cHTMLs <HEAD>

<SCRIPT LANGUAGE-'C#" RUNAT="Server"> protected void Page Load(Object sender. EventAros e) { -| DataSet datasetGoals - new DataSetpH hlestream tilestreamGoals = new

| H leitream(Server.[vlapRath(,,goals.xmr)1HleMode.upen,Hle Access Read);

StreamReader streamreaderGoals - new StreamReader(filestreamGoals ) I datasetGoals.ReadXinHstreamreaderGoalsHI |-

;"Verdana">

=;H3-Welcome to mylifetimegoals conWH3' Here are the goal categories and goal catego <P/>

CASP DATAGRID ID="datagridTitles" RUNA"

D Open

GenericTemplate.aspx from the Code Templates directory.

-0 Add a heading for the page.

> Add a DataGrid control to the page.

^0 Import the System.IO and the System.Data namespaces.

0 Add the Page_Load function to the page.

—0 Create a new DataSet object.

—□ Create a FileStream object and open the XML file on the Web server in Read mode.

Create a StreamReader object and read in the file from the FileStream object.

L-E Read the XML into the DataSet.

— Close the FileStream object.

ACCESS DATA WITH ASP.NET

If you change the index on the DataSet, you get the list of all of the goals as opposed to the goal categories.

Example:

DataView Source = new DataView(datasetGoals.Tables[1]);

ADO.NET provides disconnected data access by leveraging the simplicity and power of XML. The architecture of ADO.NET is very tightly bound to the .NET XML framework. ADO.NET and the .NET XML framework converge in the DataSet object. The native serialization format of the DataSet in XML is a perfect choice for moving data between tiers (including remote locations, like the client's browser).

XML is a key enabling technology for the .NET Platform. To create XML for your applications, you can select data out of Microsoft SQL Server 2000 as XML. Microsoft SQL Server 2000 has made enhancements to the OLE DB provider (SQLOLEDB) to allow XML documents to be set as command text and to return result sets as a stream.

ML Untitled - Notepad

File Edit Format Help

(%@ Import Namespace-'System 10" %> <%@ Import Namespace-'System .Data" %> <HTML= (HEAD;-

(SCRIPT LANGUAGE-'C#" RUNAT="Sen/er"=-protected void Page_Load(C'bject sender, EventArgs e) { DataSet datasetGoals - new DataSetQ

FileStream tilestreamGoals = new FileStream(Server.MapPath("goals xmrj.FlleMode.Open.FileAccess Read);

StreamReader streamreaderGoals - new StreamReader(filestreamGoals ) data5etGoal5.ReadXml(streainreaderGoal5); tilestreamGoals Closet)

DataView Source - new DataViewidatasetGoals.TablesjQP.I

datagridTltles.DataSource = Source; datagrid I itles.üataBindp;

(FONT FACE ="Verdana">

(H3>Welcome to mylifetimegoals.com(/H3 Here are the goal categories and goal catego

Create a new DataView and initialize it with the values read into the dataset.

Y Id jJ

E Set the DataSource for the DataGrid.

£ Save the file and request it from the Web server.

■ The goal category names and IDs appear.

£ Save the file and request it from the Web server.

■ The goal category names and IDs appear.

Was this article helpful?

0 0

Post a comment