How to read XML file in App_Data with .NET MVC3 Framework according to controller?

I need to read the XML file from App_Data to MVC3 according to the action the user is currently accessing.

<xml> <actions> <item action="index"> <add url="www.stackoverflow.com" description="This site it for learning purpouses" /> </item> </actions> </xml> 

What would be the best way to get this <item> according to the user of the action he is accessing?

EDIT


I forgot to mention that only one controller can access XML. So the file name is [controller] .xml

+7
source share
1 answer

You can use the XDocument and XPathSelectElement extension method for parsing XML:

 public ActionResult Index() { string action = RouteData.GetRequiredString("action"); string controller = RouteData.GetRequiredString("controller"); string appDataPath = Server.MapPath("~/app_data"); string file = Path.Combine(appDataPath, controller + ".xml"); var xpath = "//item[@action='" + action + "']"; var item = XDocument.Load(file).XPathSelectElement(xpath); if (item != null) { var add = item.Element("add"); var url = add.Attribute("url").Value; var description = add.Attribute("description").Value; } ... } 
+10
source

All Articles