相关疑难解决方法(0)

只有我吗?与XPath相比,我发现LINQ to XML有点麻烦

我是一名C#程序员,所以我无法利用VB中的酷XML语法.

Dim itemList1 = From item In rss.<rss>.<channel>.<item> _
                Where item.<description>.Value.Contains("LINQ") Or _
                      item.<title>.Value.Contains("LINQ")
Run Code Online (Sandbox Code Playgroud)

使用C#,我发现XPath比使用LINQ to XML执行多嵌套选择更容易思考,更容易编码,更容易理解.看看这个语法,它看起来像希腊语发誓:

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 
          }; 
Run Code Online (Sandbox Code Playgroud)

所有的演员,沉重的语法,NullPointerExceptions的可能性.XPath不会发生这种情况. …

.net linq xpath linq-to-xml

7
推荐指数
2
解决办法
563
查看次数

标签 统计

.net ×1

linq ×1

linq-to-xml ×1

xpath ×1