0

私のサンプル 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;
4

4 に答える 4

1

このアプローチを試してみると、あなたとは異なるように見えます (そして、いくつかの側面では実際に変化します) が、私の意見では、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);
于 2013-09-20T07:57:37.397 に答える
1

これを試して:

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")});
于 2013-09-20T07:29:39.147 に答える
0

この記事を参照してください:- http://www.dotnetcurry.com/ShowArticle.aspx?ID=564

foreach (XElement xcd xelement.Descendants("Id"))
    {
        Console.WriteLine((string)xcd);
    }
于 2013-09-20T07:15:18.733 に答える