Look at the XML file first:
<?XML version= "1.0" standalone= "yes"?><Student> <Stuinfo> <Stuname>Zhang Qiuli</Stuname> <Stusex>Woman</Stusex> <Stuage>18</Stuage> </Stuinfo> <Stuinfo> <Stuname>Force Mogaoyizhang</Stuname> <Stusex>Man</Stusex> <Stuage>31</Stuage> </Stuinfo> <Stuinfo> <Stuname>Lis Wen</Stuname> <Stusex>Man</Stusex> <Stuage>22</Stuage> </Stuinfo> <Stuinfo> <Stuname>Ma</Stuname> <Stusex>Woman</Stusex> <Stuage>25</Stuage> </Stuinfo> <Stuinfo> <Stuname>Sun Honglei</Stuname> <Stusex>Man</Stusex> <Stuage>32</Stuage> </Stuinfo> <Stuinfo> <Stuname>Ouyang Junshong</Stuname> <Stusex>Man</Stusex> <Stuage>28</Stuage> </Stuinfo> <Stuinfo> <Stuname>Veronica</Stuname> <Stusex>Woman</Stusex> <Stuage>23</Stuage> </Stuinfo> <Stuinfo> <Stuname>Little</Stuname> <Stusex>Woman</Stusex> <Stuage>22</Stuage> </Stuinfo></Student>
View Code
ASPX page code:
<%@ Page language="C #"autoeventwireup="true"Codebehind="Get Database data generation XML.aspx.cs"inherits="Chapter1. Getting database data Generation XML"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title> <script type="Text/javascript">function Loadxmldoc (dname) {if(window. XMLHttpRequest) {xhttp=NewXMLHttpRequest (); } Else{xhttp=NewActiveXObject ("Microsoft.XMLHTTP"); } xhttp.open ("GET", Dname,false); Xhttp.send (""); returnXhttp.responsexml; } function ReadXml () {varxmldoc = Loadxmldoc ("Student.xml"); //get the specified node varDivmsg = document.getElementById ("xmlmsg"); varmsg ="<table border= ' 1 ' id= ' mytable ' ><tr><th> name </th><th> sex </th><th> Age </th><tr>"; varnodes = Xmldoc.getelementsbytagname ("Stuinfo"); for(vari =0; i < nodes.length; i++) {msg+="<tr>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stuname")[0].firstchild.nodevalue +"</td>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stusex")[0].firstchild.nodevalue +"</td>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stuage")[0].firstchild.nodevalue +"</td>"; Msg+="</tr>"; } msg+="</table>"; Divmsg.innerhtml=msg; } </script>"Form1"runat="Server"> <div> <input type="Button"Value="JS Read XML"onclick="ReadXml ()"/><br/> <div id="xmlmsg"> </div> </div> </form></body>The above JS operation is mainly to avoid the use of childnodes (because Firefox childnodes[0] get "\ n" instead of the first child node we want, this can go to try, anyway, I have encountered this situation, so that can be compatible with IE, Firefox, Other browsers I didn't try.
JS reads XML file data and displays data in table (ie Firefox compatible)