XML ASP.Net Tutorial

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="_Default" %>



    Untitled Page


    
    

    
    

    


File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int bookcount = 0;
        decimal booktotal = 0; 
        XmlReaderSettings settings = new XmlReaderSettings();
        NameTable nt = new NameTable();
        object book = nt.Add("book");
        object price = nt.Add("price"); 
        settings.NameTable = nt;
        string booksSchemaFile = Path.Combine(Request.PhysicalApplicationPath, "Data.xsd");
        settings.Schemas.Add(null, XmlReader.Create(booksSchemaFile));
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags =
        XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;
        string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Data.xml");
        using (XmlReader reader = XmlReader.Create(booksFile, settings))
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && book.Equals(reader.LocalName)) 
                {
                    bookcount++;
                }
                if (reader.NodeType == XmlNodeType.Element && price.Equals(reader.LocalName))
                {
                    booktotal +=
                        (decimal)reader.ReadElementContentAsObject();
                }
            }
        }
        Response.Write(String.Format("Found {0} books that total {1:C}!",
            bookcount, booktotal));
    }
    void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
    {
        Response.Write(e.Message);
    }
}
File: Data.xml

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              publicationdate="1981" 
          ISBN="1-11111-11-0">
        title 1
        
            A
            B
        

        8
    
              publicationdate="1999" 
          ISBN="0-222-22222-2">
        title 2
        
            C
            D
        

        11.99
    

File: Data.xsd