Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Find duplicate xelements

I have the below XML

<Automobiles>
  <Cars>
    <YearofMfr>2010</YearofMfr>
    <Mileage>12</Mileage>
    <MeterReading>1500</MeterReading>
    <Color>Red</Color>
    <Condition>Excellent</Condition>
  </Cars>
  <Cars>
    <YearofMfr>2010</YearofMfr>
    <Mileage>12</Mileage>
    <MeterReading>1500</MeterReading>
    <Color>Red</Color>
    <Condition>Excellent</Condition>
  </Cars>
  <Cars>
    <YearofMfr>2008</YearofMfr>
    <Mileage>11</Mileage>
    <MeterReading>20000</MeterReading>
    <Color>Pearl White</Color>
    <Condition>Good</Condition>
  </Cars>
</Automobiles>

I was looking for a LINQ Query which would return duplicate nodes. In the above XML there are two nodes which are similar. The result should include both the duplicate nodes.

I also need a query which would return all the nodes which are not duplicate. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest, you create a Car class and create a list of Car instances from the XML and do your analysis on that list.
It would simplify things, because you could overwrite the Equals method of the Car class to only return true, if all properties are the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...