Fetching Complex XML Data Through C#

Introduction

 
XML stands for eXtensible Markup Language, very  similar to HTML. It is a standard flexible way of creating information formats and electronically sharing that structured data via the public Internet as well as via Corporate Networks.
 
Here I will be using a simple MVC Project to work with complex XML structure or hierarchical XML.
 
Step 1
 
Open solution and add an .xml file which contains nested details of phones like  Samsung, Apple, Vivo .
 
File Name - PhoneDetails.xml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <root>    
  3. <Phone>    
  4.    <product_name>Samsung Phone</product_name>    
  5.    <price_range>15000-30000</price_range>    
  6.    <service_centre>    
  7.       <streetnames>    
  8.          <street1>StreetA</street1>    
  9.          <street2>StreetB</street2>    
  10.       </streetnames>    
  11.       <city>Kolkata</city>    
  12.       <pin>700001</pin>    
  13.    </service_centre>    
  14. </Phone>    
  15. <Phone>    
  16.    <product_name>Apple Phone</product_name>    
  17.    <price_range>30000-50000</price_range>    
  18.    <service_centre>    
  19.       <streetnames>    
  20.          <street1>StreetC</street1>    
  21.          <street2>StreetD</street2>    
  22.       </streetnames>    
  23.       <city>Kolkata</city>    
  24.       <pin>700002</pin>    
  25.    </service_centre>    
  26. </Phone>    
  27. <Phone>    
  28.    <product_name>Vivo Phone</product_name>    
  29.    <price_range>15000-25000</price_range>    
  30.    <service_centre>    
  31.       <streetnames>    
  32.          <street1>StreetE</street1>    
  33.          <street2>StreetF</street2>    
  34.       </streetnames>    
  35.       <city>Kolkata</city>    
  36.       <pin>700003</pin>    
  37.    </service_centre>    
  38. </Phone>    
  39. </root>    
Step 2
 
Add class PhoneModelunder Models Folder and implement the following code:
  1. namespace WebApplication1.Models {  
  2.     public class PhoneModel {  
  3.         public string ProductName {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string PriceRange {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Street1 {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Street2 {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public string City {  
  20.             get;  
  21.             set;  
  22.         }  
  23.         public string Pin {  
  24.             get;  
  25.             set;  
  26.         }  
  27.         public PhoneModel() {}  
  28.         public PhoneModel(string productname, string pricerange, string street1, string street2, string city, string pin) {  
  29.             this.ProductName = productname;  
  30.             this.PriceRange = pricerange;  
  31.             this.Street1 = street1;  
  32.             this.Street2 = street2;  
  33.             this.City = city;  
  34.             this.Pin = pin;  
  35.         }  
  36.     }  
  37. }  
Step 3
 
Now go to the Controller to add the below code:
  1. using System.Collections.Generic;  
  2. using System.Web.Mvc;  
  3. using System.Xml;  
  4. using WebApplication1.Models;  
  5. namespace WebApplication1.Controllers {  
  6.         public class HomeController: Controller {  
  7.                 public ActionResult PhoneInfo() {  
  8.                     List < PhoneModel > phoneList = new List < PhoneModel > ();  
  9.                     PhoneModel phones_detail = new PhoneModel();  //You could use any of the two constructor.
  10.                     XmlDocument xdoc = new XmlDocument();  
  11.                     //Path of your .xml file  
  12.                     xdoc.Load(Server.MapPath("~/PhoneDetails.xml"));  
  13.                     XmlNodeList phones = xdoc.SelectNodes("root/Phone");  
  14.                     //Parent ForEacloop  
  15.                     foreach(XmlNode phone in phones) {  
  16.                         phones_detail.ProductName = phone["product_name"].InnerText.Trim();  
  17.                         phones_detail.PriceRange = phone["price_range"].InnerText.Trim();  
  18.                         XmlNodeList streetnames = phone.SelectNodes("service_centre/streetnames");  
  19.                         //Nested ForEach Loop  
  20.                         foreach(XmlNode streetname in streetnames) {  
  21.                             phones_detail.Street1 = streetname["street1"].InnerText.Trim();  
  22.                             phones_detail.Street2 = streetname["street2"].InnerText.Trim();  
  23.                         }  
  24.                         phones_detail.City = streetnames[0].NextSibling.InnerText;  
  25.                         phones_detail.Pin = streetnames[0].NextSibling.NextSibling.InnerText;  
  26.                         //Adding to the List  
  27.                         phoneList.Add(new PhoneModel(phones_detail.ProductName, phones_detail.PriceRange, phones_detail.Street1, phones_detail.Street2, phones_detail.City, phones_detail.Pin));  
  28.                     }  
  29.                     return View(phoneList);  
  30.                 }  
Step 4
 
Once finished with all three above steps eventually add a ViewPage to see the output .
 
Right click on the controller and select add View .
 
Note
Select "List" as Template Option and the Model class will be the class which is added in Step 2.
 
Click on add button which creates the template for us. Another approach could be selecting the Empty Template and adding the following html code manually.
  1. @model IEnumerable  
  2. <WebApplication1.Models.PhoneModel>  
  3.     <table class="table">  
  4.         <tr>  
  5.             <th>  
  6. @Html.DisplayNameFor(model => model.ProductName)  
  7. </th>  
  8.             <th>  
  9. @Html.DisplayNameFor(model => model.PriceRange)  
  10. </th>  
  11.             <th>  
  12. @Html.DisplayNameFor(model => model.Street1)  
  13. </th>  
  14.             <th>  
  15. @Html.DisplayNameFor(model => model.Street2)  
  16. </th>  
  17.             <th>  
  18. @Html.DisplayNameFor(model => model.City)  
  19. </th>  
  20.             <th>  
  21. @Html.DisplayNameFor(model => model.Pin)  
  22. </th>  
  23.             <th></th>  
  24.         </tr>  
  25. @foreach (var item in Model) {  
  26.   
  27.         <tr>  
  28.             <td>  
  29. @Html.DisplayFor(modelItem => item.ProductName)  
  30. </td>  
  31.             <td>  
  32. @Html.DisplayFor(modelItem => item.PriceRange)  
  33. </td>  
  34.             <td>  
  35. @Html.DisplayFor(modelItem => item.Street1)  
  36. </td>  
  37.             <td>  
  38. @Html.DisplayFor(modelItem => item.Street2)  
  39. </td>  
  40.             <td>  
  41. @Html.DisplayFor(modelItem => item.City)  
  42. </td>  
  43.             <td>  
  44. @Html.DisplayFor(modelItem => item.Pin)  
  45. </td>  
  46.         </tr>  
  47. }  
  48.   
  49.     </table>  
Execute the program
 
Happy Learning...