[英]Add/Remove element from XML file
I have an XML file with the following structure 我有一个具有以下结构的XML文件
<Users>
<User>
<ID>ABCD321</ID>
<Name>Mike John</Name>
<Modification>29/01/2014 16:05:34</Modification>
<LDAP-Groups>
<group Name="DOM_CLAS_48" />
</LDAP-Groups>
</User>
<User>
<ID>AXSD399</ID>
<Name>Tamy Peters</Name>
<Modification>29/01/2014 16:05:34</Modification>
<LDAP-Groups>
<group Name="DOM_SAP_208" />
<group Name="DOM_SAP_201" />
<group Name="DOM_SAP_263" />
</LDAP-Groups>
</User>
</Users>
I want to be able to Add/Remove a specific LDAP-GROUP example: remove <group Name="DOM_SAP_201" />
from the user who has ID <ID>AXSD399</ID>
and add <group Name="DNS_SAP_999" />
to the user who has the ID <ID>ABCD321</ID>
我希望能够添加/删除特定的LDAP-GROUP示例:从ID为
<ID>AXSD399</ID>
的用户中删除<group Name="DOM_SAP_201" />
<ID>AXSD399</ID>
并添加<group Name="DNS_SAP_999" />
ID为<ID>ABCD321</ID>
what i tried so far: 到目前为止我尝试过的是:
public static void UpdateLDAP(string filename, string userId)
{
XDocument xdoc = XDocument.Load(filename);
XElement user = (from xml2 in xdoc.Descendants("User")
let xElement = xml2.Element("LDAP-Groups")
where
(from xml3 in xdoc.Descendants("User")
let xUser = xml3.Element("ID")
where xUser.Value == userId.ToUpper())
select xElement).FirstOrDefault();
//........
}
You maybe first get all groups for the given user then update your XML (to be honest i don't know how to update it) 您可能首先获取给定用户的所有组,然后更新您的XML(老实说,我不知道如何更新它)
public static List<string> GetGroupsForGivenUser(XDocument xdoc, string userId)
{
var users = (from user in xdoc.Descendants("User")
let xElement = user.Element("ID")
where xElement != null && xElement.Value == userId.ToUpper()
select new
{
Id = xElement.Value,
Ldap = user.Elements("LDAP-Groups")
}).ToList();
return (
from user in users
from ldaps in user.Ldap
from ldap in ldaps.Elements("Group")
select ldap.Value
).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.