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
- <?xml version="1.0" encoding="utf-8" ?>
- <root>
- <Phone>
- <product_name>Samsung Phone</product_name>
- <price_range>15000-30000</price_range>
- <service_centre>
- <streetnames>
- <street1>StreetA</street1>
- <street2>StreetB</street2>
- </streetnames>
- <city>Kolkata</city>
- <pin>700001</pin>
- </service_centre>
- </Phone>
- <Phone>
- <product_name>Apple Phone</product_name>
- <price_range>30000-50000</price_range>
- <service_centre>
- <streetnames>
- <street1>StreetC</street1>
- <street2>StreetD</street2>
- </streetnames>
- <city>Kolkata</city>
- <pin>700002</pin>
- </service_centre>
- </Phone>
- <Phone>
- <product_name>Vivo Phone</product_name>
- <price_range>15000-25000</price_range>
- <service_centre>
- <streetnames>
- <street1>StreetE</street1>
- <street2>StreetF</street2>
- </streetnames>
- <city>Kolkata</city>
- <pin>700003</pin>
- </service_centre>
- </Phone>
- </root>
Step 2
Add class PhoneModelunder Models Folder and implement the following code:
- namespace WebApplication1.Models {
- public class PhoneModel {
- public string ProductName {
- get;
- set;
- }
- public string PriceRange {
- get;
- set;
- }
- public string Street1 {
- get;
- set;
- }
- public string Street2 {
- get;
- set;
- }
- public string City {
- get;
- set;
- }
- public string Pin {
- get;
- set;
- }
- public PhoneModel() {}
- public PhoneModel(string productname, string pricerange, string street1, string street2, string city, string pin) {
- this.ProductName = productname;
- this.PriceRange = pricerange;
- this.Street1 = street1;
- this.Street2 = street2;
- this.City = city;
- this.Pin = pin;
- }
- }
- }

Join the conversation! Your thoughts help the community grow.