Reputation: 356
I have the below XML that I want to get specific values out of using XDocument
. e.g. maxspeed.
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<meta osm_base="2015-01-15T20:26:03Z"/>
<node id="1312239864" lat="50.5348877" lon="5.6286790">
<tag k="highway" v="crossing"/>
<tag k="traffic_calming" v="bump"/>
</node>
<node id="2025084665" lat="50.5345623" lon="5.6274183">
<tag k="traffic_calming" v="choker"/>
</node>
<node id="2025084669" lat="50.5353414" lon="5.6303289"/>
<way id="191950462">
<nd ref="2025084665"/>
<tag k="bicycle" v="yes"/>
<tag k="highway" v="secondary"/>
<tag k="maxspeed" v="30"/>
<tag k="name" v="Rue d'Esneux"/>
<tag k="source:maxspeed" v="school zone"/>
</way>
</osm>
I have tried this
way.Tags = (List<OSMTag>)(doc.Descendants ("tag").Select (c => new OSMTag () {
Key = c.Attribute ("k").Value,
Value = c.Attribute ("v").Value
}));
But I get an InvalidCastException.
Below is my Object models.
public class OSMWay
{
public OSMWay (){}
public List<OSMTag> Tags { get; set; }
}
public class OSMTag
{
public OSMTag (){}
public OSMTag (string key, string value)
{
Key = key;
Value = value;
}
public string Key { get; set; }
public string Value { get; set; }
}
Upvotes: 2
Views: 685
Reputation: 4692
You get an IEnumerable back:
way.Tags = doc.Descendants ("tag").Select (c => new OSMTag () {
Key = c.Attribute ("k").Value,
Value = c.Attribute ("v").Value
}).ToList();
Should do the trick. A List implements IEnumerable but not Every IEnumerable is a List. The ToList() Extension converts a IEnumerable to a List
Upvotes: 2
Reputation: 161773
It's not a list yet:
way.Tags = (List<OSMTag>)(doc.Descendants ("tag").Select (c => new OSMTag () {
Key = c.Attribute ("k").Value,
Value = c.Attribute ("v").Value
})).ToList();
Upvotes: 1