使用LINQ从XElemnt检索数据

时间:2015-06-24 06:38:50

标签: c# xml linq

我有一个XElement如下

<row>
  <field name="field1">Test1</field>
  <field name="field2">Test2</field>
  <field name="field3">Test3</field>
</row>

我想使用LINQ使用属性值field2检索值Test2。我尝试了以下代码

var data= item.Elements("field").Single(x => x.Attribute("name").Value == "field2");

它无效。当我运行代码时,它失败并显示错误序列不包含匹配元素&gt;

我不知道我在这里失踪了什么。如何使用LINQ检索值

1 个答案:

答案 0 :(得分:0)

我发现了错误。代码应该是这样的

     -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
        MKPinAnnotationView *view = nil;
        static NSString *reuseIdentifier = @"MapView";

         view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
        if(!view) {
            view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            view.canShowCallout = YES;
            view.animatesDrop = YES;
        }
          return view;
    }
    @end
相关问题