0

2 つの Arraylist と xml ファイルがあります。この xml ファイルを LINQ ステートメントでフィルター処理したいと考えています。

これが私のコードです:

ArrayList ListOfNPSGroups = MyClass.ActiveDirectoryManager.GetGroupsInOUByValue(); //all groups from the xml

ArrayList ActiveUserList = MyClass.ActiveDirectoryManager.GetGroupmemberList(DOMAIN, Username, ListOfNPSGroups); //only the user groups

XDocument x = XDocument.Load(Server.MapPath(@"~\App_Data\location.xml"));

IEnumerable<XElement> elements;

for (int i = 0; i < ActiveUserList.Count; i++)
{
    elements = x.Descendants("plant").Where( xe => (string)xe.Attribute("group") == ActiveUserList[i].ToString());

    foreach (XElement el in elements)
    {
        el.remove();
    }
}

私のxml:

<plants>
  <plant id="DB" display=".testDB.." group="NPS_DB" />
  <plant id="EL" display=".testEL.." group="NPS_EL" />
  <plant id="IN" display="..testIN." group="NPS_IN" />
  <plant id="SB" display=".testSB.." group="NPS_SB" />
  <plant id="BL" display=".testBL.." group="NPS_BL" />
  <plant id="LS" display="..testLS.." group="NPS_LS" />
</plants>

例: ユーザーが、xml がこれに更新されたグループ nps_db および nps_el にのみ含まれている場合:

  <plants>
      <plant id="DB" display=".testDB.." group="NPS_DB" />
      <plant id="EL" display=".testEL.." group="NPS_EL" />
    </plants>

しかし、私のlinqステートメントは間違っていると思います:/

4

1 に答える 1

1

これを探していますか?

List<string> list = new List<string>{"NPS_DB", "NPS_IN"};

string xml = @"<plants>
                <plant id=""DB"" display="".testDB.."" group=""NPS_DB"" />
                <plant id=""EL"" display="".testEL.."" group=""NPS_EL"" />
                <plant id=""IN"" display=""..testIN."" group=""NPS_IN"" />
                <plant id=""SB"" display="".testSB.."" group=""NPS_SB"" />
                <plant id=""BL"" display="".testBL.."" group=""NPS_BL"" />
                <plant id=""LS"" display=""..testLS.."" group=""NPS_LS"" />
            </plants>";

var xDoc = XDocument.Parse(xml);

xDoc.Root.Descendants()
          .Where(e => !list.Contains((string)e.Attribute("group")))
          .ToList()
          .ForEach(s => s.Remove());
于 2013-10-11T13:43:02.960 に答える