难道仅仅是我吗?我发现的LINQ to XML来排序的繁琐,相比与XPath [英] Is it just me? I find LINQ to XML to be sort of cumbersome, compared to XPath
问题描述
我是一个C#程序员,所以我没有得到参加VB利用凉爽的XML语法。
I am a C# programmer, so I don't get to take advantage of the cool XML syntax in VB.
Dim itemList1 = From item In rss.<rss>.<channel>.<item> _
Where item.<description>.Value.Contains("LINQ") Or _
item.<title>.Value.Contains("LINQ")
使用C#,我发现的XPath 更容易去思考,更容易code,更容易理解,不是执行多嵌套查询使用的LINQ to XML 。看看这个语法,它看起来像希腊宣誓:
Using C#, I find XPath to be easier to think about, easier to code, easier to understand, than performing a multi-nested select using LINQ to XML. Look at this syntax, it looks like Greek swearing:
var waypoints = from waypoint in gpxDoc.Descendants(gpx + "wpt")
select new
{
Latitude = waypoint.Attribute("lat").Value,
Longitude = waypoint.Attribute("lon").Value,
Elevation = waypoint.Element(gpx + "ele") != null ?
waypoint.Element(gpx + "ele").Value : null,
Name = waypoint.Element(gpx + "name") != null ?
waypoint.Element(gpx + "name").Value : null,
Dt = waypoint.Element(gpx + "cmt") != null ?
waypoint.Element(gpx + "cmt").Value : null
};
所有铸造,重语法的可能性NullPointerExceptions的。这一切都不发生使用XPath。
All the casting, the heavy syntax, the possibility for NullPointerExceptions. None of this happens with XPath.
我喜欢LINQ在一般情况下,我用它的对象集合和数据库,但我第一次去圆与查询XML使我马上回的XPath。
I like LINQ in general, and I use it on object collections and databases, but my first go-round with querying XML led me right back to XPath.
难道仅仅是我吗?
我缺少的东西?
修改:有人投票关闭此为不是一个真正的问题。但它的是的一个真正的问题,说清楚。现在的问题是:我误解 东西的LINQ to XML?的
EDIT: someone voted to close this as "not a real question". But it is a real question, stated clearly. The question is: Am I misunderstanding something with LINQ to XML?
推荐答案
使用你感觉最舒服的,只要它能够完成任务。我用这两种方法取决于我需要做的与XML。这听起来好像你有什么LINQ是好的,什么XPath是很好的一个良好的手感。
Use what you feel the most comfortable with, as long as it gets the job done. I use both methods depending on what I need to do to with XML. It sounds to me like you've got a good handle on what LINQ is good for and what XPath is good for.
这篇关于难道仅仅是我吗?我发现的LINQ to XML来排序的繁琐,相比与XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!