I'd like to merge XML files, but without duplicates of the same XNodes. At this point if I have two identical XML files - with the same Privilege nodes - then my merge will have duplicates like this:
Input 1:
<SecurityPrivileges>
<SecurityPrivilege Principal="Jack Smith">
<Privilege Type="FileSystem" AccessType="read">c:\log</Privilege>
<Privilege Type="FileSystem" AccessType="write">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
Input 2:
<SecurityPrivileges>
<SecurityPrivilege Principal="Jack Smith">
<Privilege Type="FileSystem" AccessType="write">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
Expected output:
<SecurityPrivileges>
<SecurityPrivilege Principal="Jack Smith">
<Privilege Type="FileSystem" AccessType="read">c:\log</Privilege>
<Privilege Type="FileSystem" AccessType="write">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
Current output:
<SecurityPrivileges>
<SecurityPrivilege Principal="Jack Smith">
<Privilege Type="FileSystem" AccessType="read">c:\log</Privilege>
<Privilege Type="FileSystem" AccessType="write">c:\log</Privilege>
<Privilege Type="FileSystem" AccessType="write">c:\log</Privilege>
</SecurityPrivilege>
</SecurityPrivileges>
I try to use the Distinct
to remove duplicates, but I can't get it right.
I should check the attributes and the innertext of the XNodes. This is where I'm now:
XElement mergeSecurityPrivilege = new XElement(SECURITY_PRIVILEGES, result.Descendants(SECURITY_PRIVILEGE)
.GroupBy(x => x.Attribute(PRINCIPAL)?.Value)
.Select(x => new XElement(SECURITY_PRIVILEGE, new XAttribute(x.First().Attribute(PRINCIPAL)), x.Elements())).Distinct());