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

发布于 2024-12-20 16:03:29 字数 1205 浏览 3 评论 0原文

我有一个示例 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>

控制器名称和操作名称存储在变量中。

var cntName;
var actName;

根据这些值,我必须查找此 Xml 并获取相应的 appgroupproductcode 值。

注意:cntName 的示例值为 Activity1Activity2ActivityN
actName 的示例值为 EditActivity1EditActivity2EditActivityN

因此,根据这些值,我必须查找 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);

谢谢, 阿达什

I have a sample 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>

I have the controller name and action name stored in variables.

var cntName;
var actName;

Based on these values I have to look up this Xml and fetch the corresponding appgroup and productcode values.

Note: example values for cntName would be Activity1 or Activity2 or ActivityN.
Example values for actName would be EditActivity1 or EditActivity2 or EditActivityN.

So based on these values i have to look up the xml, Hope I am clear with my problem statement.

I am reading my xml using traditional xmldatadocument, how do i change it to LINQ? Sample below.

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

xmlDocAppMod.Load(strFileLocation);

Thanks,
Adarsh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-27 16:03:30

基本上,您有多种选择:

  1. 从磁盘加载文档并创建 xPath 表达式来评估它:

    var doc = new XmlDocument();
    doc.Load(文件名);
    var 节点 = doc.SelectSingleNode(
        string.Format("/Lookup/Controller[@Name='{0}']/action[@Name='{1}']", cntName, actName));
    
    if (节点!= null)
    {
    
            var appGroup = node.Attributes["appgroup"].Value;
            var 产品代码 = node.Attributes["产品代码"].Value;
    }
    
  2. 另一个选项可能是使用 XDocument:

     var doc = XDocument.Load("");
        var action = doc.Descendants("Controller")
            .Where(c =>;
                       {
                           var controllerName = c.Attribute("名称");
                           返回控制器名称!= null &&控制器名称.值.等于(cntName);
                       })
            .FirstOrDefault()
            .Elements("动作")
            .Where(a =>;
                       {
                           var controllerName = a.Attribute("名称");
                           返回控制器名称!= null &&控制器名称.值.等于(actName);
                       })
            .FirstOrDefault();
        var appGroup = action.Attribute("appgroup").Value;
        var 产品代码 = action.Attribute("产品代码").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;

Basically, you have several options:

  1. Load the document from disk and create an xPath expression to evaluate it:

    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;
    }
    
  2. Another option could be to use 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;
    

UPDATE:

Of course, you could use xPath with Linq-to-XML as well:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文