Reputation: 1290
Im writing an app which reads an RSS feed and places items on a map. I need to read the lat and long numbers only from this string: http://www.digitalvision.se/feed.aspx?isAlert=true&lat=53.647351&lon=-1.933506 .This is contained in link tags
Im a bit of a programming noob but im writing this in C#/Silverlight using Linq to XML.
Shold this text be extrated when parsing or after parsing and sent to a class to do this? Many thanks for your assistance.
EDIT. Im going to try and do a regex on this
this is where I need to integrate the regex somewhere in this code. I need to take the lat and long from the Link element and seperate it into two variables I can use (the results are part of a foreach loop that creates a list.)
var events = from ev in document.Descendants("item")
select new
{
Title = (ev.Element("title").Value),
Description = (ev.Element("description").Value),
Link = (ev.Element("link").Value),
};
Question is im not quite sure where to put the regex (once I work out how to use the regex properly! :-) )
Upvotes: 1
Views: 411
Reputation: 20481
try this
var url = "http://www.xxxxxxxxxxxxxx.co.uk/map.aspx?isTrafficAlert=true&lat=53.647351&lon=-1.93350";
var items = url.Split('?')[1]
.Split('&')
.Select(i => i.Split('='))
.ToDictionary(o => o[0], o => o[1]);
var lon = items["lon"];
var lat = items["lat"];
Upvotes: 1
Reputation: 3207
If you only need the Lat and Lon values and the feed is just one big XML string you can do the whole thing with a regular expression.
var rssFeed = @"http://www.xxxxxxxxxxxxxx.co.uk/map.aspx?isTrafficAlert=true&lat=53.647351&lon=-1.933506
http://www.xxxxxxxxxxxxxx.co.uk/map.aspx?isTrafficAlert=true&lat=53.647352&lon=-1.933507
http://www.xxxxxxxxxxxxxx.co.uk/map.aspx?isTrafficAlert=true&lat=53.647353&lon=-1.933508
http://www.xxxxxxxxxxxxxx.co.uk/map.aspx?isTrafficAlert=true&lat=53.647354&lon=-1.933509";
var regex = new Regex(@"lat=(?<Lat>[+-]?\d*\.\d*)&lon=(?<Lon>[+-]?\d*\.\d*)");
var latLongPairs = new List<Tuple<decimal, decimal>>();
foreach (Match match in regex.Matches(rssFeed))
{
var lat = Convert.ToDecimal(match.Groups["Lat"].Value);
var lon = Convert.ToDecimal(match.Groups["Lon"].Value);
latLongPairs.Add(new Tuple<decimal, decimal>(lat, lon));
}
Upvotes: 1