Load XML with JavaScript and send it to server

Source: Internet
Author: User

(Browser)

  1. <HtmlXmlns = "http://www.w3.org/1999/xhtml">
  2. <Head>
  3. <Title>JS loads XML files and sends them to the server</Title>
  4. <ScriptType = "text/JavaScript"><! --
  5. VaR XMLHTTP = NULL;
  6. Function xiaofang (){
  7. VaR xmldoc = new activexobject ("msxml2.domdocument. 3.0 ");
  8. Xmldoc. async = false;
  9. Xmldoc. Load ("xmlfile. xml"); // only changed here. It turns out to be loadxml ("");
  10. Sendxml (xmldoc, 'default. aspx ');
  11. }
  12. // Send the XML document to the server
  13. Function sendxml (xmldoc, serverurl ){
  14. XMLHTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
  15. VaR strdoc;
  16. If (typeof (xmldoc) = "object") // The judgment here is required. You still need to add an XML suffix here.
  17. Strdoc = xmldoc. xml;
  18. Else
  19. Strdoc = xmldoc;
  20. XMLHTTP. Open ("Post", "default. aspx", true );
  21. XMLHTTP. onreadystatechange = getdata;
  22. XMLHTTP. Send (strdoc );
  23. }
  24. Function getdata (){
  25. If (XMLHTTP. readystate = 4)
  26. {
  27. VaR strxml = XMLHTTP. responsetext;
  28. // Here, we accept XML documents sent from the server and convert them into XML documents.
  29. VaR xmldoc = new activexobject ("msxml2.domdocument. 3.0 ");
  30. Xmldoc. async = false;
  31. Xmldoc. loadxml (strxml );
  32. // Alert (xmldoc. XML );
  33. VaR singlenode = xmldoc. selectsinglenode ("/root/person [Gender = 'fang ']"); // The value must be enclosed in quotation marks.
  34. Alert (singlenode. Text );
  35. }
  36. }
  37. //--> </SCRIPT>
  38. </Head>
  39. <Body>
  40. <InputType = "button" onclick = "xiaofang ();" value = "request"/>
  41. </Body>
  42. </Html>

 

(Server)

  1.  
    1. Using system;
    2. Using system. Data;
    3. Using system. configuration;
    4. Using system. collections;
    5. Using system. Web;
    6. Using system. Web. Security;
    7. Using system. Web. UI;
    8. Using system. Web. UI. webcontrols;
    9. Using system. Web. UI. webcontrols. webparts;
    10. Using system. Web. UI. htmlcontrols;
    11. Using system. xml; (Note the namespace)
    12. Public partial class testxml_default: system. Web. UI. Page
    13. {
    14. Protected void page_load (Object sender, eventargs E)
    15. {
    16. Xmldocument xmldoc = new xmldocument ();
    17. Xmldoc. Load (request. inputstream); // receives XML from the client
    18. Xmldoc. Save (server. mappath ("~ "+"/Hello. xml "));
    19. Response. Write (xmldoc. innerxml); // return the modified XML document.
    20. Response. End ();
    21. }
    22. }

 

(XML file)

  1. <? XMLVersion = "1.0" encoding = "UTF-8"?>
  2. <Root>
  3. <PersonId = "1">
  4. <Name> Xiao</Name>
  5. <Gender> Fang</Gender>
  6. </Person>
  7. </Root>

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.