2016-03-24 7 views
0

C#(Windows Phoneの場合)のXMLファイルからデータを読み取ろうとしています。 私は、次のXMLファイルを返す:私はここにありますXMLファイルから緯度と経度の最初のインスタンスを取得しようとしていますC#のXMLファイルからデータを読み取る

private async void GetCoords2() 
    { 
     string requestURI = "https://maps.googleapis.com/maps/api/geocode/xml?address=Donegal%20Town&key=XXX"; 

     HttpWebRequest request = HttpWebRequest.Create(requestURI) as HttpWebRequest; 
     WebResponse response = await request.GetResponseAsync(); 
     using (var reader = new StreamReader(response.GetResponseStream())) 
     { 
      responseContent = reader.ReadToEnd(); 
      // Do anything with you content. Convert it to xml, json or anything. 

      ParseContent(); 
     } 
    } 

:私はオンラインからいくつかのサンプルを踏襲しているhttps://maps.googleapis.com/maps/api/geocode/xml?address=Donegal%20Town&key=XXX

、以前に私が取り組んでいたプロジェクトはありましたが、うまく動作していないようです。

latとlonの最初のインスタンスを取得するにはどうすればよいですか?

ありがとうございました。

編集:URLからキーを削除して、代わりに画像のスクリーンショットを投稿する必要がありました。

更新:現在のコードです。

void ParseContent() 
    { 
     XmlReader xmlReader = XmlReader.Create(responseContent); 
     List<string> aTitle = new List<string>(); 

     // Add as many as attributes you have in your "stop" element 

     XmlReader reader = XmlReader.Create(responseContent); 
     reader.ReadToDescendant("location"); 
     while (reader.Read()) 
     { 
      reader.MoveToFirstAttribute(); 

      reader.ReadToFollowing("lat"); 
      string latX = reader.ReadElementContentAsString(); 

      reader.ReadToFollowing("lng"); 
      string lngX = reader.ReadElementContentAsString(); 

      //reader.ReadToFollowing("Subcategory"); 
      //string subcategory = reader.ReadElementContentAsString(); 

      //reader.ReadToFollowing("Favourited"); 
      //Boolean favourited = Boolean.Parse(reader.ReadElementContentAsString()); 

      //basketxml.Add(new Pets(name, category, subcategory, description, dob, stock, price, image, id, favourited)); 

      MessageBox.Show(latX + "/" + lngX); 
     } 
    } 

ここで見つけるANSWER: http://www.superstarcoders.com/blogs/posts/geocoding-in-c-sharp-using-google-maps.aspx

+1

あなたが試したことを私たちに見せて、何を示唆しているのかわからない、あるいは誤りを見つけることができます。 – Frecklefoot

+0

@Frecklefootは現在imにコードを掲載しています。いくつかの繰り返しは、hahaを動かさないためにこの段階で削除されました。 – user3102785

+0

@ user3102785このソリューションは、ユーザーが必要とする場所がジオコードから返された結果のリストの最初の結果である場合にのみ機能します。複数の場所が返された場合(例のように)、彼らは私の答えからあなたの 'キー'を削除しても、最初に気づいていない.... :)から選択することができます場所のリスト – Monty

答えて

0

あなたから作成(....

Usings ...

using System.Collections.Generic; 
using System.IO; 
using System.Text; 
using System.Xml; 
using System.Xml.Serialization; 

クラスをこのような何かを試してみてください。 http://xmltocsharp.azurewebsites.net/を使用するXML)

[XmlRoot(ElementName = "address_component")] 
    public class Address_component 
    { 
     [XmlElement(ElementName = "long_name")] 
     public string Long_name { get; set; } 
     [XmlElement(ElementName = "short_name")] 
     public string Short_name { get; set; } 
     [XmlElement(ElementName = "type")] 
     public List<string> Type { get; set; } 
    } 

    [XmlRoot(ElementName = "location")] 
    public class Location 
    { 
     [XmlElement(ElementName = "lat")] 
     public string Lat { get; set; } 
     [XmlElement(ElementName = "lng")] 
     public string Lng { get; set; } 
    } 

    [XmlRoot(ElementName = "southwest")] 
    public class Southwest 
    { 
     [XmlElement(ElementName = "lat")] 
     public string Lat { get; set; } 
     [XmlElement(ElementName = "lng")] 
     public string Lng { get; set; } 
    } 

    [XmlRoot(ElementName = "northeast")] 
    public class Northeast 
    { 
     [XmlElement(ElementName = "lat")] 
     public string Lat { get; set; } 
     [XmlElement(ElementName = "lng")] 
     public string Lng { get; set; } 
    } 

    [XmlRoot(ElementName = "viewport")] 
    public class Viewport 
    { 
     [XmlElement(ElementName = "southwest")] 
     public Southwest Southwest { get; set; } 
     [XmlElement(ElementName = "northeast")] 
     public Northeast Northeast { get; set; } 
    } 

    [XmlRoot(ElementName = "geometry")] 
    public class Geometry 
    { 
     [XmlElement(ElementName = "location")] 
     public Location Location { get; set; } 
     [XmlElement(ElementName = "location_type")] 
     public string Location_type { get; set; } 
     [XmlElement(ElementName = "viewport")] 
     public Viewport Viewport { get; set; } 
     [XmlElement(ElementName = "bounds")] 
     public Bounds Bounds { get; set; } 
    } 

    [XmlRoot(ElementName = "result")] 
    public class Result 
    { 
     [XmlElement(ElementName = "type")] 
     public List<string> Type { get; set; } 
     [XmlElement(ElementName = "formatted_address")] 
     public string Formatted_address { get; set; } 
     [XmlElement(ElementName = "address_component")] 
     public List<Address_component> Address_component { get; set; } 
     [XmlElement(ElementName = "geometry")] 
     public Geometry Geometry { get; set; } 
     [XmlElement(ElementName = "place_id")] 
     public string Place_id { get; set; } 
    } 

    [XmlRoot(ElementName = "bounds")] 
    public class Bounds 
    { 
     [XmlElement(ElementName = "southwest")] 
     public Southwest Southwest { get; set; } 
     [XmlElement(ElementName = "northeast")] 
     public Northeast Northeast { get; set; } 
    } 

    [XmlRoot(ElementName = "GeocodeResponse")] 
    public class GeocodeResponse 
    { 
     [XmlElement(ElementName = "status")] 
     public string Status { get; set; } 
     [XmlElement(ElementName = "result")] 
     public List<Result> Result { get; set; } 
    } 

コード...

 try 
     { 
      string query1 = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?address=Donegal%20Town&key=<Your Key>"); 
      XmlDocument GeocodeResponse = new XmlDocument(); 
      GeocodeResponse.Load(query1); 

      string XMLGeocodeResponse = GeocodeResponse.InnerXml.ToString(); 
      byte[] BUFGeocodeResponse = ASCIIEncoding.UTF8.GetBytes(XMLGeocodeResponse); 
      MemoryStream ms1 = new MemoryStream(BUFGeocodeResponse); 

      XmlSerializer DeserializerPlaces = new XmlSerializer(typeof(GeocodeResponse), new XmlRootAttribute("GeocodeResponse")); 
      using (XmlReader reader = new XmlTextReader(ms1)) 
      { 
       GeocodeResponse dezerializedXML = (GeocodeResponse)DeserializerPlaces.Deserialize(reader); 

       Location LatLng = dezerializedXML.Result[0].Geometry.Location; 
      }// Put a break-point here, then mouse-over LatLng and you should have you values 
     } 
     catch (System.Exception) 
     { 
      throw; 
     } 

これは、1つのオブジェクトに全体のことをデシリアライズされます、そして、あなたはあなたが見ることができるようにします(必要な要素を選択することができます「緯度経度」は、上記が持っている二人は、あなたが抽出したい座標)...

関連する問題