Operate the responsexml attribute of the XMLHTTPRequest object in Asp.net

Source: Internet
Author: User
  1. // Deafult. ASPX page
  2. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "resultxmldemo. aspx. cs" inherits = "resultxmldemo" %>
  3. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  5. <Head runat = "server">
  6. <Title> No title page </title>
  7. <% -- XMLHttpRequest. responsexml operation methods for different browsers
  8. Currently, the most common Web browsers are IE and ff.
  9. I don't need to talk about ie anymore.
  10. FF is quite good. It supports very powerful page debugging functions.
  11. Insufficient memory consumption means too much memory consumption.
  12. Used to process Dom element attributes of XML documents
  13. Childnodes returns an array of all child elements of the current element.
  14. Firstchild returns the first sub-element of the current element.
  15. Lastchild returns the last child element of the current element.
  16. Nextsibling returns the child element that follows the current element.
  17. Nodevalue indicates the read/write attribute of the element value.
  18. Parentnode returns the parent node of the element.
  19. Previussibling returns the element adjacent to the current element.
  20. Dom element method used to traverse XML documents
  21. Getelementbyid (ID) is used to obtain the elements in the document with the specified unique ID property value.
  22. Getelementsbytagname (name) returns an array of child elements with the specified tag name in the current element.
  23. Haschildnodes () returns a Boolean value that indicates whether the element has a child element.
  24. Getattribute (name) specifies the attribute value of an element by name -- %>
  25. <Script language = "JavaScript" type = "text/JavaScript">
  26. // Create the core object of xmlhttpreqest object Ajax
  27. // Step-by-step call
  28. VaR XMLHTTP;
  29. Function createxmlhttprequest ()
  30. {
  31. // For IE
  32. If (window. activexobject)
  33. {
  34. XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
  35. }
  36. // If Firefox and other browsers have this object in ie7.0 or later versions
  37. Else
  38. {
  39. XMLHTTP = new XMLHttpRequest ();
  40. }
  41. }
  42. Function updatetotal ()
  43. {
  44. // FRM = Document. Forms [0];
  45. Url = "handler2.ashx ";
  46. // Initialize the request to the server
  47. XMLHTTP. Open ("get", URL, true); // true indicates asynchronous call.
  48. XMLHTTP. onreadystatechange = doupdate; // After the server is complete, the client processes the event
  49. // Send the request
  50. XMLHTTP. Send ();
  51. Return false;
  52. }
  53. Function doupdate ()
  54. {
  55. // Indicates that the request has been processed
  56. If (XMLHTTP. readystate = 4)
  57. {
  58. VaR xmldom = XMLHTTP. responsexml;
  59. // Xmldom. childnodes [0] is an XML Node
  60. VaR tot = Document. Forms [0]. elements ["tot"];
  61. // Tot. value = xmldom. childnodes [1]. Text; // OK
  62. // Ok2
  63. // Var root = xmldom. childnodes [1];
  64. // Var student1 = root. childnodes [0];
  65. // Var Wah = student1.childnodes [0];
  66. // Tot. value = Wah. childnodes [0]. nodevalue;
  67. // Ok2
  68. // Ok3
  69. VaR root = xmldom. childnodes [1]; // Root Node
  70. VaR Ss = xmldom. getelementsbytagname ('student ');
  71. VaR out = "";
  72. For (VAR I = 0; I <ss. length; I ++)
  73. {
  74. Out + = "" + SS [I]. getelementsbytagname ("name") [0]. childnodes [0]. nodevalue + "";
  75. }
  76. TOT. value = out;
  77. // Ok3
  78. }
  79. If (XMLHTTP. readystate = 0)
  80. {
  81. Document. Forms [0]. elements ["tot"]. value = "not initialized ";
  82. }
  83. If (XMLHTTP. readystate = 1)
  84. {
  85. Document. Forms [0]. elements ["tot"]. value = "loading ";
  86. }
  87. If (XMLHTTP. readystate = 3)
  88. {
  89. Document. Forms [0]. elements ["tot"]. value = "interactive ";
  90. }
  91. }
  92. </SCRIPT>
  93. </Head>
  94. <Body onload = "createxmlhttprequest ();">
  95. <Form ID = "form1" runat = "server">
  96. <Div>
  97. <Asp: Label id = "label3" runat = "server"> result </ASP: Label>
  98. <Input type = "text" id = "tot"
  99. Size = "30">
  100. <Div id = "namelist">
  101. </Div>
  102. <HR/>
  103. <Input type = "button" value = "Click me" onclick = "updatetotal ()"/> </div>
  104. </Form>
  105. </Body>
  106. </Html>
  107. // Handler2.aspx
  108. <% @ Webhandler Language = "C #" class = "handler2" %>
  109. Using system;
  110. Using system. Web;
  111. Public class handler2: ihttphandler {
  112. Public void processrequest (httpcontext context ){
  113. Context. response. contenttype = "text/XML ";
  114. Context. response. Write ("<? XML version = '1. 0' encoding = 'utf-8'?> <Root> <student ID = 's1'> <Name> Wah </Name> <sex> m </sex> </student> <student ID = 's2'> <name> wah2 </Name> <sex> m </sex> </student> </root> ");
  115. }
  116. Public bool isreusable {
  117. Get {
  118. Return false;
  119. }
  120. }
  121. }

 

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.