Tuesday 9 May 2017

Example of Retrieve the Data from XML File in Asp.net MVC 4 using Controller and View

Dear Student ,
   
   Please , Follow the step for retrieve the data from XML File in Asp.net MVC 4 using Controller with view

Step 1 :

   Create the XML File with DataReport.xml

  <?xml version="1.0" encoding="utf-8"?>
<CusterDetails>
  <Customer>
    <name>Deepak Kumar Gupta</name>
    <product>Buiscuits</product>
    <quantity>12</quantity>
    <price>60</price>
  </Customer>
  <Customer>
    <name>Roshan Kumar Gupta</name>
    <product>Namkeen</product>
    <quantity>120</quantity>
    <price>6000</price>
  </Customer>
  </CusterDetails>


Step 2 :

 Create the Controller file With RetrieveDataFromXMLData.cs


 public ActionResult RetriveDataFromXml()
        {
            string result = "";
            XDocument xmlDoc = XDocument.Load("C:/Users/Deepak Kumar/documents/visual studio 2012/Projects/ramkola_bhagwat/ramkola_bhagwat/XMLData/DataReport.xml");
            var q = from c in xmlDoc.Descendants("Customer")
                    select (string)c.Element("name") + "-" + (string)c.Element("product") + "-" +
                        (string)c.Element("quantity") + "-" + (string)c.Element("price");
            foreach(string ar in q)
            {
                result = result + "|" + ar;
            }
            ViewBag.aaa = result;
            return View();
        }

Step 3 :

 Create ViewFile with RetriveDataFromXml.cshtml

@model ramkola_bhagwat.Models.niitEntities

@{
    ViewBag.Title = "RetriveDataFromXml";
}

<h2>RetriveDataFromXml</h2>
<p>@ViewBag.aaa</p>


Output : 


1 comment: