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>
在上面两个不同的Xml文件中,我想比较Ist Xml的所有PartNumber和IInd Xml,并显示不匹配的PartNumber。我是C#和XmlDocument的初学者,任何帮助或提示都将不胜感激。
答案 0 :(得分:0)
首先使用PartNumbers
获取所有LINQ to XML
,然后使用Except
方法
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);
根据您的文件的内容结果将为空,因为第一个文件中不存在PartNumber
但第二个文件中不存在。如果您想要相反,则使用list2.Except(list1)