Pass Xml documents using Ajax

  • 2020-05-05 11:05:45
  • OfStack

The client

< script   language="javascript" >

// generate XML file  
function   GetAllFormData()  
{  
        var   strXML   =   " < Client > \r\n < FormData > \r\n";  
        strXML   +=   " < UserName > bccu < /UserName > "
        strXML   +=   " < Age > 25 < /Age > ";  
        strXML   +=   " < /FormData > \r\n < /Client > "
        return   strXML;  
}  

/// send the XML document  
to the server function   Send(Str,URL)    
{  
        var   Http   =   new   ActiveXObject("Microsoft.XMLHTTP")  
        Http.open("POST",URL,false)  
        Http.send(Str)  
        return   Http.responseText;  
}  

/// gets the value  
for the specified section in XML function   GetXMLNodeValue(strXML,nodeName)  
{  
        var   Dom   =   new   ActiveXObject("Microsoft.XMLDOM")  
        Dom.async=false    
        Dom.loadXML(strXML)  
        if(Dom.parseError.errorCode   !=   0)    
        {  
                delete(Dom)  
                return(false)  
        }  
        else  
        {  
                var   node   =   Dom.documentElement.selectSingleNode("//"+nodeName);  
                if(node)  
                        nodeValue   =   node.text;  
                delete(Dom)  
                return(nodeValue);  
        }  
}  

  function   Test()  
  {  
        var   tmp               =   Send(GetAllFormData(),"./test.aspx");  
        var   name             =   GetXMLNodeValue(tmp,"UserName");  
        var   password     =   GetXMLNodeValue(tmp,"Age");  
  }  
< /script >


Server side (test.cs)

System.IO.Stream   stream   =   Request.InputStream
System.Xml.XmlDocument   doc   =   new   XmlDocument();  
try  
{  
    doc. Load (stream);   // load the Xml document
sent }  
catch  
{  
    byte[]   buffer   =   new   byte[stream.Length];  
    stream.Read(buffer,0,buffer.Length);  
    string   strXML   =   System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);  
    doc.LoadXml(strXML);  
}  

// output doc to return to the client (omitted)

response.write("")

Related articles: