C# 如何从服务读取Xml到Linq
我有一个从服务获取的字符串数据 我想阅读响应文本并返回ObservableCollectionC# 如何从服务读取Xml到Linq,c#,linq-to-xml,C#,Linq To Xml,我有一个从服务获取的字符串数据 我想阅读响应文本并返回ObservableCollection public void SearchLocation(string address) { var webclient = new WebClient(); if (address != "") { string url = string.Format( @"http://de
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;
}
}
公共无效搜索位置(字符串地址)
{
var webclient=新的webclient();
如果(地址!=“”)
{
字符串url=
字符串格式(
@"http://dev.virtualearth.net/REST/v1/Locations?countryRegion=Vietnam&adminDistrict=Ha Noi&locality={0}&o=xml&key={1}“,
地址,BingMapKey);
下载StringAsync(新Uri(url));
webclient.DownloadStringCompleted+=webclient下载StringCompleted;
}
}
私有void WebClient下载StringCompleted(对象发送方,下载StringCompletedEventArgs e)
{
字符串数据=e.结果;
ReadXml(数据);
}
私有void ReadXml(字符串数据)
{
_位置。清除();
XDocument document=XDocument.Parse(数据);
变量位置=来自document.subjects(“位置”)中的loca
选择
新位置(loca.元素(“名称”)值,
loca.元素(“点”).元素(“纬度”).值“1”,
"1", "1", "1", "1");
_位置=(可观测采集)位置;
}
}
课程地点:
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ĩ độ</param>
/// <param name="longitude">kinh độ</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);
}
}
公共类位置
{
///
///TênđịađiểM
///
公共字符串名称{get;set;}
///
///Vĩđộ
///
公共双纬度{get;set;}
///
///金华ộ
///
公共双长{get;set;}
///
///Vĩđộ 不结盟运动
///
公共双南纬{get;set;}
///
///金华ộ 塔伊
///
公共双WestLongtitue{get;set;}
///
///Vĩđộ BắC
///
公共双北纬{get;set;}
///
///金华ộ 塔伊
///
公共双经度{get;set;}
///
///Khở我不能ạo位置
///
///tênđịađiểM
///vĩđộ
///金华ộ
///vĩđộ 不结盟运动
///金华ộ 塔伊
///vĩđộ BắC
///金华ộ đô
公共位置(字符串名称、字符串纬度、字符串经度、字符串南纬度、字符串西经度、,
串北纬、串东经)
{
名称=名称;
纬度=换算到双倍(纬度);
Longitute=转换为双精度(经度);
SouthLatitude=Convert.ToDouble(SouthLatitude);
北纬度=转换为双倍(北纬度);
WestLongtitue=转换为双精度(WestLongtitue);
东经度=Convert.ToDouble(东经度);
}
}
我读取并且_location返回null,哪里有错误?您可以将
IEnumerable
转换为observateCollection
,这是LINQ to XML查询的结果
_locations = (ObservableCollection<Location>) locations;
更新
代码中还存在名称空间问题。试试这个:
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");
您必须发布实际的代码,这样我们才能帮助您。位置xml中是否声明了任何名称空间,如果是,则需要使用名称空间查询xml。如果
locations
完全不返回任何元素,您的LINQ to XML查询不正确,您能帮我找到错误吗?好的,但您必须发布输入XML和Location
类结构。请参阅我的更新-您的查询中缺少XNamespace
。
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");