LINQ to XML和不同的自定义类
发布时间:2020-12-16 22:53:19 所属栏目:百科 来源:网络整理
导读:我有一个非常有趣的LINQ问题.我有一个文档,我试图过滤结果,但要过滤,我匹配来自 XML的一个元素的REGEX结果. 我有以下工作,使用LINQ to XML来获取我正在寻找的个人数据. Dim oDocument As XDocumentoDocument = XDocument.Load("test.xml")Dim results = (Fro
我有一个非常有趣的LINQ问题.我有一个文档,我试图过滤结果,但要过滤,我匹配来自
XML的一个元素的REGEX结果.
我有以下工作,使用LINQ to XML来获取我正在寻找的个人数据. Dim oDocument As XDocument oDocument = XDocument.Load("test.xml") Dim results = (From x In oDocument.Descendants.Elements("ROW") _ Select New With {.ApplicationName = GetApplicationName(x.Element("Message")),_ .EventId = x.Element("EventId")}).Distinct 但是,.Distinct没有做我想要的,它仍然显示“ApplicationName”和“EventId”的所有组合. 我最终需要的是一个不同的结果列表,一个带有应用程序名称的新对象,以及来自XML的事件id. “GetAPplicationName”是一个解析正在寻找正则表达式匹配的值的函数. 有什么指针吗? 示例XML <ROOT> <ROW> <EventId>1</EventId> <CreatedTimestamp>2009-10-28</CreatedTimestamp> <Message>There is a bunch of garbled inforamtion here and I'm trying to parse out a value Virtual Path: /MyPath then it continues on with more junk after the message,including extra stuff </Message> <!--Other elements removed for brevity --> </ROW> <ROW> <EventId>1</EventId> <CreatedTimestamp>2009-10-28</CreatedTimestamp> <Message> There is a bunch of garbled inforamtion here and I'm trying to parse out a value Virtual Path: /MyPath then it continues on with more junk after the message,including extra stuff </Message> <!--Other elements removed for brevity --> </ROW> </ROOT> 从这里我想要distinct / MyPath和EventId(在这种情况下,1条带有/ MyPath和1. 我的GetApplicationNameMethod,在此示例中将返回/ MyPath 解决方法
Distinct不知道如何比较您的商品,因此它会返回所有未过滤的商品.您应该使用
Distinct overload that implements IEqualityComparer.这将允许您比较ApplicationName和EventId属性以确定相等性.但是,这样做意味着拥有真正的类,而不是匿名类型.该文档演示了如何以易于理解的方式实现此目的.
编辑:我能够使用IEqualityComparer和EventInfo类的样本.我添加了自己的GetApplicationName实现来测试. Dim results = (From x In doc.Descendants.Elements("ROW") _ Select New EventInfo With {.ApplicationName = GetApplicationName(x.Element("Message")),_ .EventId = x.Element("EventId")}) Console.WriteLine("Total: {0}",results.Count) Console.WriteLine("Distinct Total: {0}",results.Distinct.Count) Console.WriteLine("Distinct (w/comparer) Total: {0}",results.Distinct(New EventInfoComparer()).Count) 这输出: Total: 2 Distinct Total: 2 Distinct (w/comparer) Total: 1 其余代码: ' EventInfo class and comparer Private Function GetApplicationName(ByVal element As XElement) Return Regex.Match(element.Value,"VirtualsPath:s/(w+)").Groups(1).Value End Function Public Class EventInfo Private _applicationName As String Public Property ApplicationName() As String Get Return _applicationName End Get Set(ByVal value As String) _applicationName = value End Set End Property Private _eventId As Integer Public Property EventId() As Integer Get Return _eventId End Get Set(ByVal value As Integer) _eventId = value End Set End Property End Class Public Class EventInfoComparer Implements IEqualityComparer(Of EventInfo) Public Function Equals1(ByVal x As EventInfo,ByVal y As EventInfo) As Boolean _ Implements IEqualityComparer(Of EventInfo).Equals ' Check whether the compared objects reference the same data. If x Is y Then Return True ' Check whether any of the compared objects is null. If x Is Nothing OrElse y Is Nothing Then Return False ' Check whether the EventInfos' properties are equal. Return (x.ApplicationName = y.ApplicationName) AndAlso (x.EventId = y.EventId) End Function Public Function GetHashCode1(ByVal eventInfo As EventInfo) As Integer _ Implements IEqualityComparer(Of EventInfo).GetHashCode ' Check whether the object is null. If eventInfo Is Nothing Then Return 0 ' Get the hash code for the ApplicationName field if it is not null. Dim hashEventInfoAppName = _ If(eventInfo.ApplicationName Is Nothing,eventInfo.ApplicationName.GetHashCode()) ' Get the hash code for the EventId field. Dim hashEventInfoId = eventInfo.EventId.GetHashCode() ' Calculate the hash code for the EventInfo. Return hashEventInfoAppName Xor hashEventInfoId End Function End Class (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 由Ajax请求一般处理程序下载文件引发的问题后的一些总结
- ruby-on-rails – 使用Wicked_PDF渲染为PDF时,ChartKick图表
- c# – 体系结构:依赖注入,松散耦合的程序集,实现隐藏
- c# – 改进MS图表控件的自定义标记图像性能
- xmltodict 同级目录下 两个相同node 去除
- 其实xml解析很快的。很方便的,用superxmlparse
- VB备忘录(22)数据控件及实例
- swift – iOS 9中的requestLocation()多次调用didUpdateLoc
- objective-c – 如何仅流式传输来自YouTube的视频的声音?
- c# – 在MVC3中的每个Action之前运行一个方法