Read XML file in mvc 4
Read XML File Using XDocument or XmlDocument
Controller Actionprivate XDocument ReadXML() { XDocument xDoc = new XDocument(); xDoc = XDocument.Load(HttpContext.Server.MapPath("~/App_Data/LocationsXML_Prod.XML")); return xDoc; } private XmlDocument ReadXMLDoc() { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(HttpContext.Server.MapPath("~/App_Data/LocationsXML_Prod.XML")); return xmldoc; } private void GetCountryList() { IList<selectlistitem> _result = new List<selectlistitem>(); XmlNodeList defaults = ReadXMLDoc().GetElementsByTagName("CountryOrRegion"); foreach (XmlNode node in defaults) { _result.Add(new SelectListItem { Value = node.Attributes["Code"].InnerText, Text = node.Attributes["Name"].InnerText, }); } ViewBag.Country = _result; } public JsonResult GetStateList(string countryCode) { IList<selectlistitem> _result = new List<selectlistitem>(); IEnumerable<xelement> defaults = ReadXML().Descendants("CountryOrRegion"); var nodes = from e in defaults where e.Attribute("Code").Value.Equals(countryCode) from element in e.Elements() select element; foreach (var node in nodes) { _result.Add(new SelectListItem { Value = (string)node.Attribute("Code"), Text = (string)node.Attribute("Name"), }); } return Json(_result, JsonRequestBehavior.AllowGet); }
Read XML file in mvc 4
Reviewed by Bhaumik Patel
on
7:09 AM
Rating: