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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<script type="text/javascript" language="javascript">
var XmlHttp = false;

GetXmlHttpRequest();

function OnClick()
{
    if (XmlHttp)
    {  
      XmlHttp.open("GET", "MyData.xml", true);
      XmlHttp.onreadystatechange = Callback;
      XmlHttp.send(null);
    }
}

function Callback()
{
   if (XmlHttp.readyState == 4)
   {     
      if (XmlHttp.status == 200 )
      {        
        var xmldoc = XmlHttp.responseXML.documentElement;
        var text = XmlHttp.responseText;
        var div1 =  document.getElementById("Div1");
        div1.innerText = text;             
        alert("Done.");
     }
     else
     {
       alert("Requested resource not found");
     }
   }
}
  
function GetXmlHttpRequest()
{
  try
  {       
     XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
  }
  catch (e)
  {
     GetIEXmlHttpRequest();
  }
}

function GetIEXmlHttpRequest()
{
  try
  {    
     //Internet Explorer
     XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");     
  }
  catch (e)
  {
     GetMozillaXmlHttpRequest();
  }
}

function GetMozillaXmlHttpRequest()
{
   try
     {
        //Mozilla browsers
        XmlHttp  = new XmlHttpRequest();
     }
     catch(ex)
     {
        alert("Failed to get XmlHttpRequest");
        XmlHttp = false;
     }
}
</script>

<head runat="server">
    <title>Get XML data using XmlHttpRequest</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Get Data" OnClientClick="OnClick()" />
    <div id="Div1">        
    </div>
    </form>
</body>
</html>
