JavaScript code for loading xml files and sending them to the server

Source: Internet
Author: User

Front-end:
The Code is as follows:
<! 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">
<Head>
<Title> js loads xml files and sends them to the server </title>
<Script type = "text/javascript"> <! --
Var xmlHttp = null;
Function f (){
Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
XmlDoc. async = false;
XmlDoc. load ("xmlfile. xml"); // only changed here. It turns out to be loadXML ("");
SendXml (xmlDoc, 'default. aspx ');
}
// Send the Xml document to the server
Function sendXml (xmlDoc, serverURL ){
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP. 3.0 ");
Var strDoc;
If (typeof (xmlDoc) = "object") // The judgment here is required. You still need to add an xml suffix here.
StrDoc = xmlDoc. xml;
Else
StrDoc = xmlDoc;
XmlHttp. open ("POST", "Default. aspx", true );
XmlHttp. onreadystatechange = getData;
XmlHttp. send (strDoc );
}
Function getData (){
If (xmlHttp. readyState = 4)
{
Var strxml = xmlHttp. responseText;
// Here, we accept Xml documents sent from the server and convert them into xml documents.
Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
XmlDoc. async = false;
XmlDoc. loadXML (strxml );
// Alert (xmlDoc. xml );
Var singleNode = xmlDoc. selectSingleNode ("/root/person [gender = 'male']"); // The value must be enclosed in quotation marks.
Alert (singleNode. text );
}
}
// --> </Script>
</Head>
<Body>
<Input type = "button" onclick = "f ();" value = "request"/>
</Body>
</Html>
<! 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">
<Head>
<Title> js loads xml files and sends them to the server </title>
<Script type = "text/javascript"> <! --
Var xmlHttp = null;
Function f (){
Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
XmlDoc. async = false;
XmlDoc. load ("xmlfile. xml"); // only changed here. It turns out to be loadXML ("");
SendXml (xmlDoc, 'default. aspx ');
}
// Send the Xml document to the server
Function sendXml (xmlDoc, serverURL ){
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP. 3.0 ");
Var strDoc;
If (typeof (xmlDoc) = "object") // The judgment here is required. You still need to add an xml suffix here.
StrDoc = xmlDoc. xml;
Else
StrDoc = xmlDoc;
XmlHttp. open ("POST", "Default. aspx", true );
XmlHttp. onreadystatechange = getData;
XmlHttp. send (strDoc );
}
Function getData (){
If (xmlHttp. readyState = 4)
{
Var strxml = xmlHttp. responseText;
// Here, we accept Xml documents sent from the server and convert them into xml documents.
Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
XmlDoc. async = false;
XmlDoc. loadXML (strxml );
// Alert (xmlDoc. xml );
Var singleNode = xmlDoc. selectSingleNode ("/root/person [gender = 'male']"); // The value must be enclosed in quotation marks.
Alert (singleNode. text );
}
}
// --> </Script>
</Head>
<Body>
<Input type = "button" onclick = "f ();" value = "request"/>
</Body>
</Html>

Ajax
The Code is as follows:
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;
Public partial class testXml_Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (Request. InputStream); // receives xml from the client
Xmldoc. Save (Server. MapPath ("~ "+"/Hello. xml "));
Response. Write (xmldoc. InnerXml); // return the modified Xml document.
Response. End ();
}
}
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;
Public partial class testXml_Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (Request. InputStream); // receives xml from the client
Xmldoc. Save (Server. MapPath ("~ "+"/Hello. xml "));
Response. Write (xmldoc. InnerXml); // return the modified Xml document.
Response. End ();
}
}

Xml document:
The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Root>
<Person id = "1">
<Name> tree </name>
<Gender> male </gender>
</Person>
</Root>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.