C# 按XAttribute和x27合并XDocuments;s值
我已经设法合并了具有相同C# 按XAttribute和x27合并XDocuments;s值,c#,xml,linq,C#,Xml,Linq,我已经设法合并了具有相同XAttribute值的XElement-s,但我不知道如何找到附加XDocument中没有的列表 <!--xml1--> <SecurityPrivileges> <SecurityPrivilege Principal="user1"> <Privilege Type="FileSystem" AccessType="read" Action="Add">c:\log</Privilege>
XAttribute
值的XElement
-s,但我不知道如何找到附加XDocument
中没有的列表
<!--xml1-->
<SecurityPrivileges>
<SecurityPrivilege Principal="user1">
<Privilege Type="FileSystem" AccessType="read" Action="Add">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
<!--xml2-->
<SecurityPrivileges>
<SecurityPrivilege Principal="user2">
<Privilege Type="FileSystem" AccessType="read" Action="Add">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
<!--expected result-->
<SecurityPrivileges>
<SecurityPrivilege Principal="user1">
<Privilege Type="FileSystem" AccessType="read" Action="Add">c:\log</Privilege>
</SecurityPrivilege>
<SecurityPrivilege Principal="user2">
<Privilege Type="FileSystem" AccessType="read" Action="Add">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
我已经弄明白了。它不像我想的那么容易阅读
foreach (XElement appendElement in append.Root.Elements())
{
string appendPrincipal = appendElement.Attribute(PRINCIPAL).Value;
//If the result does not contain an XElement with this attribute
if (!result.Root.Elements()
.Any(x => x.Attribute(PRINCIPAL).Value.Equals(appendPrincipal)))
{
result.Root.Add(appendElement);
}
}
建议将xml转换为字典,合并字典中的数据并将其转换回xml作为输出。@qxg我更愿意将其保留为XDocument。在此过程中,我之前有一个字典转换,我不想再次转换。当它们仍然是字典时,是否可以合并它们。@qxg不幸的是,否。这是不同的步骤,处于不同的阶段。
foreach (XElement appendElement in append.Root.Elements())
{
string appendPrincipal = appendElement.Attribute(PRINCIPAL).Value;
//If the result does not contain an XElement with this attribute
if (!result.Root.Elements()
.Any(x => x.Attribute(PRINCIPAL).Value.Equals(appendPrincipal)))
{
result.Root.Add(appendElement);
}
}