How To Read xml using JQuery

How To Read xml using JQuery

In this post, I will show how to read XML using JQuery

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 runat="server">
   <title></title>

   <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

   <script type="text/javascript">

       $(document).ready(function() {
           $.get('Data.xml', function(d) {
               var data = "";
               var startTag = "<table border='1' id='mainTable'><tbody><tr><td style=\"width: 120px\">Name</td><td style=\"width: 120px\">Link</td></tr>";
               var endTag = "</tbody></table>";
               $(d).find('url').each(function() {
                   var $url = $(this);
                   var link = $url.find('link').text();
                   var name = $url.find('name').text();
                   data += '<tr><td>' + name + '</td>';
                   data += '<td>' + link + '</td></tr>';
               })
               $("#content").html(startTag + data + endTag);
               ;
           });

       });
   </script>

</head>
<body>
   <div id="content">
   </div>
</body>
</html>

Data.xml

<?xml version="1.0" encoding="utf-8" ?>
<Urls>
 <url>
   <name>google</name>
   <link>
     www.google.com
   </link>
 </url>
 <url>
   <name>aspdotnetcodebook</name>
   <link>http://aspdotnetcodebook.blogspot.com</link>
 </url>
</Urls>

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post