xmlファイルの子ノードから値を取得できないようです。私はすべてを試したような気がします。私が望むのは、xmlファイルのlocation
子ノードのlatitude
とlongitude
の値を取得することだけです。私は間違って何をしていますか?おそらく、私はXMLの代わりにJSONを試してみるべきでしょう。XMLを使用してDescendants()またはElement()から値を取得できません
private void RequestCompleted(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(result);
StreamReader stream = new StreamReader(response.GetResponseStream());
try
{
XDocument xdoc = XDocument.Load(stream);
XElement root = xdoc.Root;
XNamespace ns = xdoc.Root.Name.Namespace;
List<XElement> results = xdoc.Descendants(ns + "GeocodeResponse").Descendants(ns + "result").ToList();
List<XElement> locationElement = results.Descendants(ns + "geometry").Descendants(ns + "location").ToList();
List<XElement> lat = locationElement.Descendants(ns + "lat").ToList();
List<XElement> lng = locationElement.Descendants(ns + "lng").ToList();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message)
}
}
また、ここでは、XML
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>134 Gearger Circle, Lexington, KY, USA</formatted_address>
<geometry>
<location>
<lat>36.31228546</lat>
<lng>-91.4444399</lng>
</location>
<location_type>ROOFTOP</location_type>
</geometry>
<place_id>ChIJtwDV05mW-IgRyJKZ7fjmYVc</place_id>
</result>
</GeocodeResponse>
はゼロの数を示してデバッグ値です。それが何を意味するかわからない。私はちょうどlat
とlng
の値が必要です。
イメージにはjsonデータが含まれています。あなたの応答ストリームがXml – benPearce
を返すと確信しています。私は間違った写真を含んだ。私はちょうどそれを固定した。 – daveskylark
画像には、「」を囲む ''タグがあります。あなたはそれを取り除きましたか?どうやって? xDoc.Root.Nameとは何ですか? –