Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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
C# 无法选择相关嵌套节点:LINQ_C#_Linq - Fatal编程技术网

C# 无法选择相关嵌套节点:LINQ

C# 无法选择相关嵌套节点:LINQ,c#,linq,C#,Linq,我的示例XML如下所示: <?xml version="1.0" encoding="utf-8"?> <Root> <RoleSecurity Name="A" Workflowstatus ="B"> <Accountgroup Name = "Group1"> <Attribute ID="12345" Name="Sample1"/> <Attribute ID="12445" Name

我的示例XML如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <RoleSecurity Name="A" Workflowstatus ="B">
    <Accountgroup Name = "Group1">
      <Attribute ID="12345" Name="Sample1"/>
      <Attribute ID="12445" Name="Sample2"/>
    </Accountgroup>
    <Accountgroup Name = "Group2">
      <Attribute ID="12345" Name="Sample1"/>
      <Attribute ID="12445" Name="Sample2"/>
    </Accountgroup>
  </RoleSecurity>
</Root>

我正在尝试枚举和提取与特定角色名称、工作流状态和帐户组对应的所有ID

我的LINQ查询正在根据角色名称选择节点。但我无法继续下去。 请帮忙

这是我到现在为止的LINQ代码

XElement xcd = XElement.Load(strFileName);
IEnumerable<XElement> enumCust = from cust in xcd.Elements("RoleSecurity")
           where (string)cust.Attribute("Name") == strRole
           select cust;
XElement xcd=XElement.Load(strFileName);
IEnumerable enumCust=来自xcd.Elements中的cust(“角色安全”)
其中(字符串)客户属性(“名称”)==strRole
选择客户;
XElement xcd=XElement.Load(strFileName);
IEnumerable enumCust=来自xcd.Root.Elements中的cust(“角色安全”)
其中cust.Attribute(“Name”).Value==strRole
选择客户;
现在应该可以了 您缺少根节点下方枚举所需的
.Root

.Value
要检索指定属性的字符串值,请参阅本文:-

试试这个:

string roleName = "A";
string workflowStatus = "B";
string accountGroup = "Group1";

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Root>
        <RoleSecurity Name=""A"" Workflowstatus =""B"">
        <Accountgroup Name = ""Group1"">
            <Attribute ID=""12345"" Name=""Sample1""/>
            <Attribute ID=""12445"" Name=""Sample2""/>
        </Accountgroup>
        <Accountgroup Name = ""Group2"">
            <Attribute ID=""12345"" Name=""Sample1""/>
            <Attribute ID=""12445"" Name=""Sample2""/>
        </Accountgroup>
        </RoleSecurity>
    </Root>";

XElement element = XElement.Parse(xml);

var ids = element.Elements("RoleSecurity")
    .Where(
        e =>
            (string) e.Attribute("Name") == roleName &&
            (string) e.Attribute("Workflowstatus") == workflowStatus)
    .Elements("Accountgroup").Where(e => (string) e.Attribute("Name") == accountGroup)
    .Elements("Attribute")
    .Select(e => new {ID = (string) e.Attribute("ID"), Name = (string) e.Attribute("Name")});
string roleName=“A”;
字符串workflowStatus=“B”;
字符串accountGroup=“Group1”;
字符串xml=@“
";
XElement元素=XElement.Parse(xml);
变量ID=元素。元素(“角色安全”)
.在哪里(
e=>
(字符串)e.Attribute(“Name”)==roleName&&
(字符串)e.Attribute(“Workflowstatus”)==Workflowstatus)
.Elements(“Accountgroup”)。其中(e=>(字符串)e.Attribute(“Name”)==Accountgroup)
.要素(“属性”)
.Select(e=>new{ID=(string)e.Attribute(“ID”)、Name=(string)e.Attribute(“Name”)});

尝试使用这种方法,似乎与您的不同(在某些方面它确实发生了变化),但在我看来,流畅地使用LINQ查询解析XML文件是一种很好的方法,它遵循XML节点顺序,并且很容易理解:

  XElement element = XElement.Load(strFileName);

  var linqList = element.Elements("RoleSecurity")
                              .Where(entry => entry.Attribute("Name").Value == "A" && 
                               entry.Attribute("Workflowstatus").Value == "B")
                                  .Descendants("Accountgroup")
                                  .Where(x => x.Attribute("Name").Value == "Group1")
                                     .Descendants("Attribute")
                                     .SelectMany(id => id.Attribute("ID").Value);

我正试着往更深的地方钻。正如我在问题中提到的,我正在尝试枚举和提取与特定角色名称、工作流状态和帐户组对应的所有ID。非常感谢!奇迹已经发生,代码正在工作!:)关于
.Element
函数的一个问题。您已经使用它来查询多个级别。这是怎么回事?我想你误解了什么。首先,我使用
元素
,它返回给定节点的
IEnumerable
。仔细看代码。代码通过过滤必要的内容一层一层地遍历。谢谢你,哈姆雷特。。。我意识到,在每一个“你去一个层次的深度。只是想知道,
后代的方法有何不同?此解决方案由另一个SO用户提供。需要谷歌搜索。
子体
返回当前级别中任何级别的所有匹配节点。感谢您的回答!我已经实施了公认答案的方法。我只是想知道哪种方法对我来说更好,为什么?一个非常有用的讨论(元素和分散)[
string roleName = "A";
string workflowStatus = "B";
string accountGroup = "Group1";

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Root>
        <RoleSecurity Name=""A"" Workflowstatus =""B"">
        <Accountgroup Name = ""Group1"">
            <Attribute ID=""12345"" Name=""Sample1""/>
            <Attribute ID=""12445"" Name=""Sample2""/>
        </Accountgroup>
        <Accountgroup Name = ""Group2"">
            <Attribute ID=""12345"" Name=""Sample1""/>
            <Attribute ID=""12445"" Name=""Sample2""/>
        </Accountgroup>
        </RoleSecurity>
    </Root>";

XElement element = XElement.Parse(xml);

var ids = element.Elements("RoleSecurity")
    .Where(
        e =>
            (string) e.Attribute("Name") == roleName &&
            (string) e.Attribute("Workflowstatus") == workflowStatus)
    .Elements("Accountgroup").Where(e => (string) e.Attribute("Name") == accountGroup)
    .Elements("Attribute")
    .Select(e => new {ID = (string) e.Attribute("ID"), Name = (string) e.Attribute("Name")});
  XElement element = XElement.Load(strFileName);

  var linqList = element.Elements("RoleSecurity")
                              .Where(entry => entry.Attribute("Name").Value == "A" && 
                               entry.Attribute("Workflowstatus").Value == "B")
                                  .Descendants("Accountgroup")
                                  .Where(x => x.Attribute("Name").Value == "Group1")
                                     .Descendants("Attribute")
                                     .SelectMany(id => id.Attribute("ID").Value);