To compare the innertag values of two different Xml, and get the differences using XmlDocument

StackOverflow https://stackoverflow.com/questions/21871585

  •  13-10-2022
  •  | 
  •  

문제

Xml1:-

<Demo name="Demo" MoneyFormat="" Type="" PricingCountry="IND" PricingCurrency="UD" PricingTerm="DP" PricingLang="99" >
    <Part ProductId="XFY-12" PartNumber="328806" Price="0.05" Description="ABCDE" />
    <Part ProductId="MER-14" PartNumber="328807" Price="0.85"  Description="FGHIJ" />
    <Part ProductId="MEM-24" PartNumber="328808" Price="72.87"  Description="KLMN" />
</Demo>

Xml2:-

<list version="1.0">
  <category name="XYZ"  psmax="0" idle="15" max="10">
    <imagename>sample.gif</imagename>  
    <chassis partnumber="328806">DellInspiron</chassis>
    <ComponentCollection>
      <component type="Processors" rel="OR" maxprocessors="2" minprocselect="1">
        <item type="Quad Core">
          <citem partnumber="328807" value="ABC"  idle="11" max="70" maxselect="2" />
          <citem partnumber="667421" value="DEF"  idle="11" max="70"  maxselect="2" />
        </item>
        <item type="Six Core">
          <citem partnumber="667376" value="GHE" idle="15" max="85"  maxselect="2" />
          <citem partnumber="667375" value="HIJ"   idle="14" max="92"  maxselect="2" />
          <citem partnumber="667424" value="KLM"  idle="14" max="64"  maxselect="2" />
          <citem partnumber="328808" value="NOP"  idle="16" max="106" maxselect="2" />
        </item>        
      </component>
</ComponentCollection>
</category>
</list>

In the above two different Xml files, I want to compare all the PartNumber of Ist Xml with IInd Xml and display the unmatched PartNumber. I am a beginner in C# and XmlDocument, Any help or hint would be appreciated.

도움이 되었습니까?

해결책

First get all PartNumbers with LINQ to XML then use Except method

var list1 = XDocument.Load("firstpath")
            .Descendants()
            .SelectMany(x => x.Attributes())
            .Where(x => x.Name == "PartNumber")
            .Select(x => (string)x);
var list2 = XDocument.Load("secondpath")
            .Descendants()
            .SelectMany(x => x.Attributes())
            .Where(x => x.Name == "partnumber")
            .Select(x => (string)x);

var result = list1.Except(list2);

According to your file's content result will be empty because there is no PartNumber that exist in first file but not exist in second.If you want the opposite then use list2.Except(list1)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top