2013-10-21 3 views
0

우선, 모든 질문에 대해 인내심을 갖고 감사드립니다. 여기에서 검색했습니다. Why doesn't this code find any duplicates within an xml element?remove a duplicate element(with specific value) from xml using linq 이며 가까이에 있지만 찾아 볼 수는 없습니다.linq 및 C#의 중복 코드 제거

XML에서 중복 요소를 제거해야합니다. 이러한 요소가 존재할 수도 있고 없을 수도 있습니다.

XML 조각은 다음과 같습니다. 중복 BuildNumber 요소를 제거해야합니다. 다음과 같이

<?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> 

}

내 코드는

// 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(); 

사람이 올바른 방향으로 날 포인트?

답변

0
XmlFile.Descendants("Session") 
     .SelectMany(s => s.Elements("BuildNumber").Skip(1)) 
     .Remove(); 

이 쿼리는 각 세션의 첫 번째 BuldNumber 요소를 제외한 모든 요소를 ​​선택하여 제거합니다. 따라서 첫 번째 BuildNumber 요소 만 각 Session 요소에 남아 있습니다.

+0

이, 물론, 당신은 각 세션의 * 한 * BuildNumber 있다고 가정합니다. 그러나 여러 BuildNumbers가 서로 다른 값을 가진 경우 허용됩니다 ... –

+0

안녕하세요 - 대단히 감사합니다. 이 디버깅을 위해 사용하고 내 콘솔 응용 프로그램에서 작동합니다. 나는 SSIS에서 지금 작동하게 만들고 어떤 이유로 어떤 문제에 부딪 히고있다. –

+0

@LeviCalhoun 어떤 종류의 문제가 있습니까? –

2
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(); 

또는 IEnumerable<XNode>.Remove() 확장 방법을 사용하여이 :

xDoc.Root.Elements("Session") 
     .SelectMany(s => s.Elements("BuildNumber") 
          .GroupBy(b => (int)b) 
          .SelectMany(g => g.Skip(1))).Remove(); 
+0

답장을 보내 주셔서 감사합니다. 심각하게 고맙습니다. 나는이 일을 절대적으로 끝내야했다. –