2013-10-06 2 views
0

많은 중복이 포함 된 현재 XML이 있습니다.중복 XML 노드 찾기 및 XML strcutre 및 크기 최소화

<XML version="1.0"> 
    <body> 
    <optionTree> 
     <device modelDescription="20 Slot MX2000 Chassis, Base with 1 RE, SFBs, Fan Trays, AC Power" ProductId="1542671" modelCatalogNum="MX2020-BASE-AC" price="450000" quantity="1" userRemovable="1"> 
     <optionItem modelDescription="MPC Slot 0" slotIndex="1" isSingleSelected="1"> 
      <device modelDescription="2xTrio Chipset Enhanced MPC, 1588v2, port queuing, price includes full scale L2/L2.5 and reduced scale L3 features" ProductId="1540947" modelCatalogNum="MX-MPC2E-3D-P" price="70000"> 
      <optionItem modelDescription="MIC Slot 1" slotIndex="1" isSingleSelected="1"> 
       <device modelDescription="20x10/100/1000 MIC for MX, requires optics sold separately" ProductId="334748" modelCatalogNum="MIC-3D-20GE-SFP" price="9000"> 
       <optionItem modelDescription="SFP Slot 1" slotIndex="1" isSingleSelected="1"> 
        <device modelDescription="SFP capable of support 10/100/1000 speeds" ProductId="291564" modelCatalogNum="SFP-1GE-FE-E-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable supporting 1000BASE-EX Gigabit Ethernet Optic Module, 40km." ProductId="205359" modelCatalogNum="SFP-GE40KM" price="2500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-SX Gigabit Ethernet Optic Module" ProductId="51804" modelCatalogNum="SFP-1GE-SX" price="500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-T Gigabit Ethernet Module (uses Cat 5 cable)" ProductId="51805" modelCatalogNum="SFP-1GE-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LH Gigabit Ethernet Optic Module" ProductId="205257" modelCatalogNum="SFP-1GE-LH" price="5995" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LX Gigabit Ethernet Optic Module" ProductId="51803" modelCatalogNum="SFP-1GE-LX" price="995" /> 
       </optionItem> 
       <optionItem modelDescription="SFP Slot 2" slotIndex="2" isSingleSelected="1"> 
        <device modelDescription="SFP capable of support 10/100/1000 speeds" ProductId="291564" modelCatalogNum="SFP-1GE-FE-E-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable supporting 1000BASE-EX Gigabit Ethernet Optic Module, 40km." ProductId="205359" modelCatalogNum="SFP-GE40KM" price="2500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-SX Gigabit Ethernet Optic Module" ProductId="51804" modelCatalogNum="SFP-1GE-SX" price="500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-T Gigabit Ethernet Module (uses Cat 5 cable)" ProductId="51805" modelCatalogNum="SFP-1GE-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LH Gigabit Ethernet Optic Module" ProductId="205257" modelCatalogNum="SFP-1GE-LH" price="5995" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LX Gigabit Ethernet Optic Module" ProductId="51803" modelCatalogNum="SFP-1GE-LX" price="995" /> 
       </optionItem> 
       <optionItem modelDescription="SFP Slot 3" slotIndex="3" isSingleSelected="1"> 
        <device modelDescription="SFP capable of support 10/100/1000 speeds" ProductId="291564" modelCatalogNum="SFP-1GE-FE-E-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable supporting 1000BASE-EX Gigabit Ethernet Optic Module, 40km." ProductId="205359" modelCatalogNum="SFP-GE40KM" price="2500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-SX Gigabit Ethernet Optic Module" ProductId="51804" modelCatalogNum="SFP-1GE-SX" price="500" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-T Gigabit Ethernet Module (uses Cat 5 cable)" ProductId="51805" modelCatalogNum="SFP-1GE-T" price="395" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LH Gigabit Ethernet Optic Module" ProductId="205257" modelCatalogNum="SFP-1GE-LH" price="5995" /> 
        <device modelDescription="Small Form Factor Pluggable 1000Base-LX Gigabit Ethernet Optic Module" ProductId="51803" modelCatalogNum="SFP-1GE-LX" price="995" /> 
       </optionItem> 
       </device> 
      </optionItem> 
      </device> 
     </optionItem> 
     </device> 
    </optionTree> 
    </body> 
</XML> 

이 XML을 최소화하고 중복 된 XML 노드를 모두 제거하고자합니다. 이 XML을 통해

  • 루프를하고 "장치"요소를 중복 찾습니다

    나는 코드 다음을 수행하는 수행을 만들려고합니다.

  • 각 중복 노드에 대해 "ProductId"속성을 제외한 모든 속성을 제거하십시오.
  • 다음과 같이 범례를 작성하십시오.
  • "Devices"라는 새 요소가 작성되어 제거 된 모든 XML 노드의 전체 세부 사항을 포함합니다.

최종 XML은 다음과 같아야합니다

<XML version="1.0"> 
    <body> 
    <optionTree> 
     <device modelDescription="20 Slot MX2000 Chassis, Base with 1 RE, SFBs, Fan Trays, AC Power" ProductId="1542671" modelCatalogNum="MX2020-BASE-AC" price="450000" quantity="1" userRemovable="1"> 
     <optionItem modelDescription="MPC Slot 0" slotIndex="1" isSingleSelected="1"> 
      <device modelDescription="2xTrio Chipset Enhanced MPC, 1588v2, port queuing, price includes full scale L2/L2.5 and reduced scale L3 features" ProductId="1540947" modelCatalogNum="MX-MPC2E-3D-P" price="70000"> 
      <optionItem modelDescription="MIC Slot 1" slotIndex="1" isSingleSelected="1"> 
       <device modelDescription="20x10/100/1000 MIC for MX, requires optics sold separately" ProductId="334748" modelCatalogNum="MIC-3D-20GE-SFP" price="9000"> 
       <optionItem modelDescription="SFP Slot 1" slotIndex="1" isSingleSelected="1"> 
        <device ProductId="291564" /> 
        <device ProductId="205359" /> 
        <device ProductId="51804" /> 
        <device ProductId="51805" /> 
        <device ProductId="205257" /> 
        <device ProductId="51803" /> 
       </optionItem> 
       <optionItem modelDescription="SFP Slot 2" slotIndex="2" isSingleSelected="1"> 
        <device ProductId="291564" /> 
        <device ProductId="205359" /> 
        <device ProductId="51804" /> 
        <device ProductId="51805" /> 
        <device ProductId="205257" /> 
        <device ProductId="51803" /> 
       </optionItem> 
       <optionItem modelDescription="SFP Slot 3" slotIndex="3" isSingleSelected="1"> 
        <device ProductId="291564" /> 
        <device ProductId="205359" /> 
        <device ProductId="51804" /> 
        <device ProductId="51805" /> 
        <device ProductId="205257" /> 
        <device ProductId="51803" /> 
       </optionItem> 
       </device> 
      </optionItem> 
      </device> 
     </optionItem> 
     </device> 
    </optionTree> 
    <devices> 
     <device ProductId="291564" modelDescription="SFP capable of support 10/100/1000 speeds" modelCatalogNum="SFP-1GE-FE-E-T" price="395" /> 
     <device ProductId="205359" modelDescription="Small Form Factor Pluggable supporting 1000BASE-EX Gigabit Ethernet Optic Module, 40km." modelCatalogNum="SFP-GE40KM" price="2500" /> 
     <device ProductId="51804" modelDescription="Small Form Factor Pluggable 1000Base-SX Gigabit Ethernet Optic Module" modelCatalogNum="SFP-1GE-SX" price="500" /> 
     <device ProductId="51805" modelDescription="Small Form Factor Pluggable 1000Base-T Gigabit Ethernet Module (uses Cat 5 cable)" modelCatalogNum="SFP-1GE-T" price="395" /> 
     <device ProductId="205257" modelDescription="Small Form Factor Pluggable 1000Base-LH Gigabit Ethernet Optic Module" modelCatalogNum="SFP-1GE-LH" price="5995" /> 
     <device ProductId="51803" modelDescription="Small Form Factor Pluggable 1000Base-LX Gigabit Ethernet Optic Module" modelCatalogNum="SFP-1GE-LX" price="995" /> 
    </devices> 
    </body> 
</XML> 

은 이미 XML 노드를 중복 발견 코드를 발견했다, 그러나 나는 전술 한 바와 같이 단지 그 중복을 찾을 수보다 더 많은 일을하고 싶다. 당신의 문제를 해결해야

답변

0

신속하고 더러운 솔루션 :

//find duplicates 
var duplicates = doc.Descendants("device") 
        .GroupBy(n => n.ToString()) 
        .Where(g => g.Count() > 1) 
        .ToArray(); 

//remove attributes on all duplicates 
foreach (var attribute in duplicates.SelectMany(g => g) 
            .SelectMany(el => el.Attributes() 
                 .Where(attribute => attribute.Name != "ProductId")) 
            .ToArray()) 
{ 
    attribute.Remove(); 
} 

//add new element 
doc.Descendants("body") 
    .Single() 
    .Add(new XElement("devices", duplicates.Select(g => XElement.Parse(g.Key)))); 

은 기본적으로 중복 IGrouping 컬렉션 키로 요소의 문자열 표현을 사용하고 모든 중복 노드를 보유하고 있습니다.