Working with XML in ASP.Net

Leave a Comment

The eXtensive Markup Language has a very important role in web applications. .Net has given enough space for XML classes in its framework. The main classes working with XML data are as follows:

XmlTextReader

The XmlTextReader class is an implementation of XmlReader.

  • It provides a fast, performant parser.
  • It enforces the rules that XML must be well formed.
  • It is neither a validating nor a non-validating parser since it does not have DTD or schema information.
  • It can read text in blocks, or read characters from a stream.
  • The XmlTextReader performs the following functions:
  • Enforces the rules that XML must be well-formed.
  • Checks that the DTD is well-formed. However, does not use the DTD for validation, expanding entity references, or adding default attributes.
  • Validating is not done against DTDs or Schemas.
  • Checks the correct formation of any DOCTYPE nodes.
  • Checks the correct formation of the entities. For node types of EntityReference, a single empty EntityReference node is returned. An empty EntityReference node is one in which its Value property is a string. Empty. This is because there is no DTD or schema to expand the entity reference. The XmlTextReader does ensure that the whole DTD is well-formed, including the EntityReference nodes.
  • Provides a performant XML parser, because the XmlTextReader does not havethe overhead involved with validation checking.


XMLTextWriter

Represents a writer that provides a fast, non-cached, forward the only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.

XMLDocument

This class implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. The DOM is an in-memory (cache) tree representation of an XML document that enables the navigation and editing of this document.

XmlDataDocument

This class Allows structured data to be stored, retrieved, and manipulated through a relational DataSet.

XmlNodeReader

The XmlNodeReader has the ability to read an XML DOM sub tree. This class does not support DTD or schema validation.

Document Navigator

This class enables to navigate an XML document represented by XmlDocument class.

XML with Dataset

In the previous chapters DataSet was used with SqlServer. A DataSet can also be used with XML data. The following example shows how to read an XML document into a Dataset.


Reading an XML document into a DataSet

A XML file directly can be directly read from the hard drive into DataSet by using the ReadXml () method of the DataSet class. The following example demonstrates it:

Example

Items.xml

<Items>
<FoodItems>
<Food>
Toast
</Food>
<Price>
14.45
</Price>
</FoodItems>
<FoodItems>
<Food>
Noodles
</Food>
<Price>
30.00
</Price>
</FoodItems>
</Items>

ReadXmlItems.aspx

<%@ Import Namespace="System.Data" %>
 <Script Runat="Server">
Sub Page_Load
Dim dstMenu As DataSet
dstMenu = New DataSet()
dstMenu.ReadXml( MapPath( "Items.xml" ))
dgrdMenu.DataSource = dstMenu
dgrdMenu.DataBind ()
End Sub
</Script>
<html>
    <head>
      <title>ReadXmlItems.aspx</title>
   </head>
   <body>
       <asp:DataGrid ID="dgrdMenu" cellpadding="10" Runat="Server" />
   </body>
</html>



0 comments:

Post a Comment