XML ASP.Net Tutorial

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



    Xml Validation


    
    
                         runat="server" 
                     Text="Use Data.xml" 
                     Checked="True" 
                     GroupName="Valid">
        
                    runat="server" 
                Text="Validate XML" 
                OnClick="cmdValidate_Click">
        
    

  

    
    

    


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.Xml.Schema;
using System.IO;
using System.Xml;
public partial class XmlValidation : System.Web.UI.Page
{
  protected void cmdValidate_Click(object sender, EventArgs e)
  {
    string filePath = "Data.xml";
      lblStatus.Text = "";
    XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add("yourURI",Request.PhysicalApplicationPath + "Data.xsd");
    settings.ValidationType = ValidationType.Schema;
    settings.ValidationEventHandler += new ValidationEventHandler(ValidateHandler);
    FileStream fs = new FileStream(filePath, FileMode.Open);
    
    XmlReader r = XmlReader.Create(fs, settings);
    while (r.Read())
    {
    }
    fs.Close();
    lblStatus.Text += "Complete.";
  }
  public void ValidateHandler(Object sender, ValidationEventArgs e)
  {
    lblStatus.Text += "Error: " + e.Message + "";
  }
}
File: Data.xml


    
        49.33
    
    
        43398.55
    
    
        49.99
    

File: Data.xsd

    targetNamespace="yourURI"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"  >