efficiently removing duplicate xml elements in c#

前端 未结 3 1000
天命终不由人
天命终不由人 2020-12-22 01:20

I have couple of XML files that contain lots of duplicate entries, such as these.


  

        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-22 01:47

    There's a couple of things that you could do here. As well as the other answers so far, you can note that Distinct() has an overload that takes an IEqualityComparer. You could use something like this ProjectionEqualityComparer to do something like this:

    var images = xdoc.Descendants("image")
        .Distinct(ProjectionEqualityComparer.Create(xe => xe.Attributes("location").First().Value))
    

    ... which would give you all of the unique "image" elements that have unique location attributes.

提交回复
热议问题