Reputation: 45
First of all, I thank everyone for his or her patience with my questions. I searched here Why doesn't this code find any duplicates within an xml element? and remove a duplicate element(with specific value) from xml using linq and am close, but not getting it.
I need to remove duplicate elements in the XML. These elements may or may not exist
The XML snippet is as follows. In need to remove the duplicate BuildNumber elements.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ProductSessions FileID="{C7DCB747-AB3A-4222-B14B-F7A7994C212F}">
<Session LicenceNumber="E2240A66AC64CB770000" SessionGuid="{20c5d49e-7442-4fd0-b612-23aa743f4bd9}" FK_FileId="{C7DCB747-AB3A-4222-B14B-F7A7994C212F}">
<TimeOpened>2013/10/14 11:18:43</TimeOpened>
<LicenseInfo Configuration="XYZ" Description="Company Standard Config+More" DongleID="-error-no-dongle-" LicenseKey="FLEXlm Server Licence" Licensed="Company USA" FK_Sess ionGuid="{20c5d49e-7442-4fd0-b612-23aa743f4bd9}" />
<ProductVersion>Product 9.0.0 NTx86-64 (build 987)</ProductVersion>
<BuildNumber>987</BuildNumber>
<ProductArchitecture>NTx86-64</ProductArchitecture>
<ProductVersion>9.0.0</ProductVersion>
<SystemInfo OperativeSystem="Microsoft Windows 8 Enterprise Edition (build 9200) 64-bit" User=" " FK_SessionGuid="{20c5d49e-7442-4fd0-b612-23aa743f4bd9}" />
<ApplicationName>X</ApplicationName>
<TimeClosed>2013/10/14 11:42:57</TimeClosed>
</Session>
<Session LicenceNumber="E2240A66AC64CB770000" SessionGuid="{5682f705-baa1-46c0-a5ca- 3c6d816c94cc}" FK_FileId="{C7DCB747-AB3A-4222-B14B-F7A7994C212F}">
<TimeOpened>2013/10/14 11:55:23</TimeOpened>
<LicenseInfo Configuration="XYZ" Description="Company Standard Config+More" DongleID="-error-no-dongle-" LicenseKey="FLEXlm Server Licence" Licensed="Company USA" FK_SessionGuid="{5682f705-baa1-46c0-a5ca-3c6d816c94cc}" />
<ProductVersion>Product 8.2.x NTx86-64 (build 123)</ProductVersion>
<BuildNumber>123</BuildNumber>
<BuildNumber>123</BuildNumber>
<BuildNumber>123</BuildNumber>
<ProductArchitecture>NTx86-64</ProductArchitecture>
<ProductVersion>8.2.x</ProductVersion>
<SystemInfo OperativeSystem="Microsoft Enterprise Edition (build 9200) 64-bit" User=" " FK_SessionGuid="{5682f705-baa1-46c0-a5ca-3c6d816c94cc}" />
<ApplicationName>X</ApplicationName>
<TimeClosed>2013/10/14 11:58:20</TimeClosed>
</Session>
}
My code is as follows
// This gets the correct # of sessions
IEnumerable<XElement> childElements =
from element in XmlFile.Elements().Descendants("Session")
select element;
foreach (XElement el in childElements)
{
var dups = XmlFile.Descendants(el.n).GroupBy(e => e.Descendants("BuildNumber").First().ToString());
//remove the duplicates
foreach (XElement ele in dups.SelectMany(g => g.Skip(1)))
ele.Remove();
Can anyone point me in the right direction?
Upvotes: 0
Views: 2551
Reputation: 125640
var xDoc = XDocument.Load("Input.xml");
var duplicates = xDoc.Root
.Elements("Session")
.SelectMany(s => s.Elements("BuildNumber")
.GroupBy(b => (int)b)
.SelectMany(g => g.Skip(1)))
.ToList();
foreach (var item in duplicates)
item.Remove();
Or using IEnumerable<XNode>.Remove()
extension method:
xDoc.Root.Elements("Session")
.SelectMany(s => s.Elements("BuildNumber")
.GroupBy(b => (int)b)
.SelectMany(g => g.Skip(1))).Remove();
Upvotes: 2
Reputation: 236268
XmlFile.Descendants("Session")
.SelectMany(s => s.Elements("BuildNumber").Skip(1))
.Remove();
This query selects all but first BuldNumber elements from each Session and removes them. Thus only first BuildNumber element will stay in each Session element.
Upvotes: 0