Sunday, August 11, 2019

How to Fetch and Show Data from XML file using Jquery

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add an XML file into Project.

Right Click on Solution Explorer > Add > New existing item > Select XML File > Add.

Step-3: Add a Webpage and Design for fetch and show data using Jquery.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

Jquery Code 
  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function () {
  4. $("#btnGetData").click(function () {
  5. $("#UpdatePanel").html("Please wait...");
  6. $.ajax({
  7. url: "/cd_catalog.xml",
  8. type: "GET",
  9. dataType: "xml",
  10. success: OnSuccess,
  11. error: OnError
  12. });
  13. });
  14. });
  15.  
  16. function OnSuccess(xml) {
  17. var tableContent = "<table border='1' cellspacing='0' cellpadding='5'>" +
  18. "<tr>" +
  19. "<th>TITLE</th>" +
  20. "<th>ARTIST</th>" +
  21. "<th>COUNTRY</th>" +
  22. "<th>COMPANY</th>" +
  23. "<th>PRICE</th>" +
  24. "<th>YEAR</th>" +
  25. "</tr>";
  26. $(xml).find('CD').each(function () {
  27. tableContent += "<tr>" +
  28. "<td>" + $(this).find('TITLE').text() + "</td>" +
  29. "<td>" + $(this).find('ARTIST').text() + "</td>" +
  30. "<td>" + $(this).find('COUNTRY').text() + "</td>" +
  31. "<td>" + $(this).find('COMPANY').text() + "</td>" +
  32. "<td>" + $(this).find('PRICE').text() + "</td>" +
  33. "<td>" + $(this).find('YEAR').text() + "</td>" +
  34. "</tr>";
  35. });
  36. tableContent += "</table>";
  37. $("#UpdatePanel").html(tableContent);
  38. }
  39.  
  40. function OnError(data) {
  41. $("#UpdatePanel").html("Error! Please try again.");
  42. }
  43.  
  44. </script>
HTML Code 
  1. <h3>Populate Data from XML file using Jquery</h3>
  2. <input id="btnGetData" type="button" value="Populate Data from XML File" />
  3. <div id="UpdatePanel" style="padding:20px 10px">
  4. </div>

Step-4: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...