jason jason - 17 days ago 5
C# Question

Parse a string which is HTML input

Is there an elegant way to get lat and lon values from this string using C#? Thanks.
String is like this :

<input type="hidden" name="myinput" id="myinput" value='{"lat":11.111111,"lon":22.222222}'>

Answer

Per my comment, this is how I'd do it. I'm not claiming to be awesome at Regex by any means. There's probably a better way.

var r = new Regex(@"""lat"":(?<lat>\d+\.\d+),""lon"":(?<lon>\d+\.\d+)");
var m = r.Match(@"<input type=""hidden"" name=""myinput"" id=""myinput"" value='{""lat"":11.111111,""lon"":22.222222}'>");
if ( m.Success )
{
    Console.WriteLine(f.Groups["lat"]);
    Console.WriteLine(f.Groups["lon"]);
}
Comments