XDocument in MVC4RFRick Freeman11yAsked Aug 31, 2015, 3:24 PM EDTSolvedShareLinkedInXCopy linkSavehttp://www.c-sharpcorner.com/UploadFile/dhananjaycoder/reading-xml-file-through-linq-a-few-tips/Hello,Can you provide an example of how to do this in MVC4?Thanks,Rick
XDocument in MVC4RFRick Freeman11yAsked Aug 31, 2015, 3:24 PM EDTSolvedShareLinkedInXCopy linkSavehttp://www.c-sharpcorner.com/UploadFile/dhananjaycoder/reading-xml-file-through-linq-a-few-tips/Hello,Can you provide an example of how to do this in MVC4?Thanks,Rick
Manas MohapatraRank #182 · 13.2K reputation11y agoPosted Sep 1, 2015, 9:02 PM EDT✓ Accepted answer+1XDocument document = XDocument.Load(@"D:\MyDemo\MyDemo\ConsoleApplication1\XMLFile1.xml");// If you have xml data in a variable then use this one// XDocument document = XDocument.Parse(""); var tempList = (from r in document.Descendants("game").Where (r => (string)r.Attribute("id") == "53a41428-d8f7-4020-97cc-13792e6994eb") select new { id = r.Attribute("id").Value, venue = r.Element("venue").Attribute("name").Value, home = r.Element("home").Attribute("name").Value, away = r.Element("away").Attribute("name").Value, status = r.Attribute("status").Value, scheduled = r.Attribute("scheduled").Value }).ToList(); foreach (var r in tempList) { Response.Write(r.id + ", " + r.venue + "," + r.away + "," + r.status + "," + r.scheduled); } If you are facing any problem please put it here. +1
Manas MohapatraRank #182 · 13.2K reputation11y agoPosted Aug 31, 2015, 9:04 PM EDT+1Please go through below link: http://www.c-sharpcorner.com/UploadFile/xiankaylle/read-data-from-xml-in-Asp-Net-mvc-5/ +1
RFRick Freeman33 reputation11y agoPosted Sep 1, 2015, 10:26 AM EDT+1Here's my xml doc: My class: public class Games { public List games { get; set; } public string id { get; set; } public string home { get; set; } public string away { get; set; } public string venue { get; set; } public string status { get; set; } public string scheduled { get; set; } } code: var games = new List(); games = (from rows in ds.Tables[0].AsEnumerable() select new Games { id = rows[1].ToString(), venue = rows[0].ToString(), home = rows[0].ToString(), away = rows[0].ToString(), status = rows[2].ToString(), scheduled = rows[6].ToString(), }).ToList(); return games; Your method only returns values according the the rows[0]. I need the values of each element i.e " Please provide example... +1RFRick Freeman33 reputation11y agoPosted Sep 2, 2015, 10:28 AM EDT+1My xml doc is returned with these two elements that are effecting the query:<league xmlns="http://feed.elasticstats.com/schema/basketball/schedule-v2.0.xsd" id="4353138d-4c22-4396-95d8-5f587d2df25c" name="NBA" alias="NBA"> <season-schedule id="5ddc217a-a958-4616-9bdf-e081022c440b" year="2013" type="REG"> ect...Since were using xdoc.Descendants("game") I should be able to query the doc even if these elements are included, however I get a null... The class also includes a "root" reference: [Serializable] [XmlRoot("game"), XmlType("game")] public class Games { When I remove the two elements the query works!+1Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
RFRick Freeman33 reputation11y agoPosted Sep 2, 2015, 10:28 AM EDT+1My xml doc is returned with these two elements that are effecting the query:<league xmlns="http://feed.elasticstats.com/schema/basketball/schedule-v2.0.xsd" id="4353138d-4c22-4396-95d8-5f587d2df25c" name="NBA" alias="NBA"> <season-schedule id="5ddc217a-a958-4616-9bdf-e081022c440b" year="2013" type="REG"> ect...Since were using xdoc.Descendants("game") I should be able to query the doc even if these elements are included, however I get a null... The class also includes a "root" reference: [Serializable] [XmlRoot("game"), XmlType("game")] public class Games { When I remove the two elements the query works!+1
Manas MohapatraRank #182 · 13.2K reputationPosted Sep 1, 2015, 9:02 PM EDT
Manas MohapatraRank #182 · 13.2K reputationPosted Aug 31, 2015, 9:04 PM EDT
Rick Freeman33 reputationPosted Sep 1, 2015, 10:26 AM EDT
Rick Freeman33 reputationPosted Sep 2, 2015, 10:28 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts