0

我有一个从服务位置 Xml获取的字符串数据

我想阅读响应文本并返回 ObservableCollection

public void SearchLocation(string address)
{
var webclient = new WebClient();
        if (address != "")
        {
            string url =
                string.Format(
                    @"http://dev.virtualearth.net/REST/v1/Locations?countryRegion=Vietnam&adminDistrict=Ha Noi&locality={0}&o=xml&key={1}",
                    address, BingMapKey);
            webclient.DownloadStringAsync(new Uri(url));
            webclient.DownloadStringCompleted += WebclientOnDownloadStringCompleted;
        }
    }

    private void WebclientOnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string data = e.Result;
        ReadXml(data);
    }

    private void ReadXml(string data)
    {
        _locations.Clear();


        XDocument document = XDocument.Parse(data);
        var locations = from loca in document.Descendants("Location")
                            select
                                new Location(loca.Element("Name").Value,
                                            loca.Element("Point").Element("Latitude").Value,"1",
                                             "1", "1", "1", "1");

            _locations = (ObservableCollection<Location>) locations;

    }
}

上课地点:

public class Location
{
    /// <summary>
    /// Tên địa điểm
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// Vĩ độ
    /// </summary>
    public double Latitude { get; set; }

    /// <summary>
    /// Kinh độ
    /// </summary>
    public double Longitute { get; set; }

    /// <summary>
    /// Vĩ độ Nam
    /// </summary>
    public double SouthLatitude { get; set; }

    /// <summary>
    /// Kinh độ Tây
    /// </summary>
    public double WestLongtitue { get; set; }

    /// <summary>
    /// Vĩ độ Bắc
    /// </summary>
    public double NorthLatitude { get; set; }

    /// <summary>
    /// Kinh độ Tây
    /// </summary>
    public double EastLongitude { get; set; }

    /// <summary>
    /// Khởi tạo Location
    /// </summary>
    /// <param name="name">tên địa điểm</param>
    /// <param name="latitue">vĩ độ&lt;/param>
    /// <param name="longitude">kinh độ&lt;/param>
    /// <param name="southLatitude">vĩ độ nam</param>
    /// <param name="westLongitude">kinh độ tây</param>
    /// <param name="northLatitude">vĩ độ bắc</param>
    /// <param name="eastLongitude">kinh độ đông</param>
    public Location(string name, string latitue, string longitude, string southLatitude, string westLongitude,
                    string northLatitude, string eastLongitude)
    {
        Name = name;
        Latitude = Convert.ToDouble(latitue);
        Longitute = Convert.ToDouble(longitude);
        SouthLatitude = Convert.ToDouble(southLatitude);
        NorthLatitude = Convert.ToDouble(northLatitude);
        WestLongtitue = Convert.ToDouble(westLongitude);
        EastLongitude = Convert.ToDouble(eastLongitude);
    }
}

我阅读并且 _location 返回 null,错误在哪里?

4

1 回答 1

1

您可以将IEnumerable<Location>LINQ to XML 查询的结果ObservableCollection<Location>直接转换为。

_locations = (ObservableCollection<Location>) locations;

只需将查询结果集合中的所有元素添加到现有的ObservableCollection

foreach(var location in locations)
    _locations.Add(location);

更新

您的代码中也存在命名空间问题。试试那个:

XDocument document = XDocument.Parse(data);

var ns = XNamespace.Get("http://schemas.microsoft.com/search/local/ws/rest/v1");

var locations = from loca in document.Descendants(ns + "Location")
                select
                    new Location(loca.Element(ns + "Name").Value,
                                loca.Element(ns + "Point").Element(ns + "Latitude").Value, "1",
                                 "1", "1", "1", "1");
于 2013-04-04T10:42:41.540 回答