Си диез / Говнокод #11870 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
private string ExtractNodeValue(string text, string nodeName)
{
    string result = string.Empty;

    int slength = ("<" + nodeName + ">").Length;
    int sindex = text.IndexOf("<" + nodeName + ">");
    int eindex = text.IndexOf("</" + nodeName + ">");

    if (sindex > 0 && eindex > 0)
        result = text.Substring(sindex + slength, eindex - sindex - slength);

    return result;
}



string request = string.Format("http://maps.google.com/maps/geo?ll={0},{1}&hl=en&output=xml&key=abcdefg", location.latitude, location.longitude);
Logger.Log(request);
HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(request);
WebResponse responce = httprequest.GetResponse();
Stream str = responce.GetResponseStream();
XmlTextReader reader = new XmlTextReader(str);
reader.XmlResolver = null;
XmlDocument doc = new XmlDocument();
doc.Load(reader);
str.Close();
reader.Close();

XmlNodeList listResponse = doc.ChildNodes[1].ChildNodes[0].ChildNodes;
foreach (XmlNode nodePlace in listResponse)
{
    if (nodePlace.Name == "Placemark")
    {
        string text = nodePlace.InnerXml;

        string Country = ExtractNodeValue(text, "CountryName");
        if ((this.DataContext.Countries.Count(x => x.Name == location.countryName) == 0 || string.IsNullOrWhiteSpace(location.countryName)) &&
            !string.IsNullOrWhiteSpace(Country))
        {
            location.countryName = Country;
        }

        string Region = ExtractNodeValue(text, "AdministrativeAreaName");
        if (this.DataContext.States.Count(x => x.AlphaCode == location.region || x.Name == location.region) == 0 &&
            !string.IsNullOrWhiteSpace(Region))
        {
            location.region = Region;
        }

        string City = ExtractNodeValue(text, "LocalityName");
        if (this.DataContext.Cities.Count(x => x.Name == location.city) == 0 &&
            !string.IsNullOrWhiteSpace(City))
        {
            location.city = City;
        }
        break;
    }
}

отличный парсиг xml.

Запостил: mangyst mangyst, (Updated )

Комментарии (2) RSS

Добавить комментарий

Переведи на "PHP", guest!

    А не использовать ли нам bbcode?


    8