0

嗨,我对 xaml 和 xdocument 的工作有疑问。

我的应用程序:

我有一个 DropDownList 控件,我希望它填充一个 xml 文件,但我希望用户只能看到特殊值。我的意思是他必须在一个活动目录组中。所以我想在我的代码中加载 xml 并使用过滤器值更新它并将其加载到下拉列表中。

这是我的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>

这是我的代码:

ArrayList ActiveUserList = MyClass.GetGroupmemberList(DOMAIN, Username);

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

            int index = ActiveUserList.Count;

            ArrayList DropDownList = new ArrayList();

            for (int i = 0; i < index; i++)
            {
                IEnumerable<XElement> att = from el in x.Descendants("plant")
                                            where (string)el.Attribute("group") == ActiveUserList[i].ToString()
                                            select el;

                //????
            }

如何使用找到的 Xelements 更新 xml 并使用它创建一个新的 xml 并将其加载到我的下拉列表中。

如果我的用户名在组 nps_db 和 nps_el 中,我想只看到:

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

1 回答 1

1

我没有获取匹配列表,而是删除了不匹配列表。以这种方式获得所需的 XML 作为输出似乎更直接。

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/xml";

    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>
    ";

    XDocument x = XDocument.Parse(xml);
    string[] ActiveUserList = { "NPS_DB", "NPS_BL" };

    var att = x.Descendants("plant").Where(el => !ActiveUserList.Contains(el.Attribute("group").Value));

    att.Remove();

    context.Response.Write(x.ToString());
}
于 2013-10-11T14:42:54.967 回答