Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在控制器中读取xml和查找?_Xml_Asp.net Mvc 3_Xmlnode_Xml Attribute - Fatal编程技术网

在控制器中读取xml和查找?

在控制器中读取xml和查找?,xml,asp.net-mvc-3,xmlnode,xml-attribute,Xml,Asp.net Mvc 3,Xmlnode,Xml Attribute,我有一个xml示例 <Lookup> <Controller Name="Activity1" > <action Name="Editactivity1" appgroup="Something" productcode="SomethingElse"/> </Controller> <Controller Name="Activity2"> <action

我有一个xml示例

<Lookup> 
    <Controller Name="Activity1" >
       <action Name="Editactivity1" appgroup="Something" productcode="SomethingElse"/>    
    </Controller> 
    <Controller Name="Activity2">    
       <action Name="Editactivity2" appgroup="Something1" productcode="SomethingElse2"/>  
    </Controller>
</Lookup>
基于这些值,我必须查找此Xml并获取相应的appgroup和productcode值

注意:cntName的示例值为Activity1或Activity2或ActivityN。 actName的示例值为EditActivity1或EditActivity2或EditActivityN

因此,基于这些值,我必须查找xml,希望我的问题陈述清楚

我正在使用传统的xmldatadocument阅读xml,如何将其更改为LINQ?下面是示例

XmlDocument xmlDocAppMod = new XmlDocument();
strFileName = System.Configuration.ConfigurationManager.AppSettings["AppModOptListPath"].ToString();
strFileLocation = System.Web.HttpContext.Current.Server.MapPath("~/" + strFileName);

xmlDocAppMod.Load(strFileLocation);
谢谢,
Adarsh

基本上,您有几种选择:

从磁盘加载文档并创建xPath表达式以对其求值:

var doc = new XmlDocument();
doc.Load(fileName);
var node = doc.SelectSingleNode(
    string.Format("/Lookup/Controller[@Name='{0}']/action[@Name='{1}']", cntName, actName));

if (node != null)
{

        var appGroup = node.Attributes["appgroup"].Value;
        var productcode = node.Attributes["productcode"].Value;
}
另一种选择是使用XDocument:

    var doc = XDocument.Load("");
    var action = doc.Descendants("Controller")
        .Where(c =>
                   {
                       var controllerName = c.Attribute("Name");
                       return controllerName != null && controllerName.Value.Equals(cntName);
                   })
        .FirstOrDefault()
        .Elements("action")
        .Where(a =>
                   {
                       var controllerName = a.Attribute("Name");
                       return controllerName != null && controllerName.Value.Equals(actName);
                   })
        .FirstOrDefault();
    var appGroup = action.Attribute("appgroup").Value;
    var productCode = action.Attribute("productcode").Value;
更新:

当然,您也可以将xPath与Linq to XML结合使用:

var doc = XDocument.Load("");
var action = (XElement) doc.XPathEvaluate(string.Format("/Lookup/Controller[@Name='{0}']/action[@Name='{1}']", cntName, actName));
var appGroup = action.Attribute("appgroup").Value;
var productCode = action.Attribute("productcode").Value;

actName是Name属性的值,对吗?那么cntName是什么?嗯,有两个不同的名称属性,一个用于外部级别的控制器,另一个用于控制器节点内部的操作,请让我知道它是否错误,并且必须为其指定不同的名称哦,好的!没有先看到查找标记吗?mvc标记是否表示ASP.NET mvc?谢谢,您能在此详细说明//读取其他值吗?
var doc = XDocument.Load("");
var action = (XElement) doc.XPathEvaluate(string.Format("/Lookup/Controller[@Name='{0}']/action[@Name='{1}']", cntName, actName));
var appGroup = action.Attribute("appgroup").Value;
var productCode = action.Attribute("productcode").Value;