簡體   English   中英

在C#中從XML文件讀取數據

[英]Reading data from XML file in C#

我正在嘗試從C#(對於Windows Phone)中的XML文件讀取數據。 我返回以下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();
        }
    }

我正在嘗試從XML文件中檢索緯度和經度的第一個實例,該文件位於以下位置: 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);
        }
    }

在這里找到答案: http : //www.superstarcoders.com/blogs/posts/geocoding-in-c-sharp-using-google-maps.aspx

嘗試這樣的事情。

正在使用...

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;
        }

這會將整個對象反序列化為單個對象,然后您可以選擇所需的元素(如上面的“ LatLng”所示,具有要提取的兩個坐標)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM