C# 获取深度嵌入的XML元素值
我正在尝试获取地址的纬度/经度,并使用dev.virtualearth.net上的XML提供程序 XML如下所示:C# 获取深度嵌入的XML元素值,c#,asp.net,xml,xml-parsing,C#,Asp.net,Xml,Xml Parsing,我正在尝试获取地址的纬度/经度,并使用dev.virtualearth.net上的XML提供程序 XML如下所示: <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"> &l
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>2</EstimatedTotal>
<Resources>
<Location>
<Name>350 Avenue V, New York, NY 11223</Name>
<Point>
<Latitude>40.595024898648262</Latitude>
<Longitude>-73.969506248831749</Longitude>
</Point>
但是对于纬度和经度值,我只是得到了null
我做错了吗?您也应该将名称空间与嵌套元素一起使用
string xmlString =
@"
<Response xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schemas.microsoft.com/search/local/ws/rest/v1"">
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>2</EstimatedTotal>
<Resources>
<Location>
<Name>350 Avenue V, New York, NY 11223</Name>
<Point>
<Latitude>40.595024898648262</Latitude>
<Longitude>-73.969506248831749</Longitude>
</Point>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
";
var doc = XDocument.Parse(xmlString);
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
var positions = doc.Descendants(ns + "Point")
.Select(p =>
new {
Latitude = (double)p.Element(ns + "Latitude"),
Longitude = (double)p.Element(ns + "Longitude")
});
string xmlString=
@"
200
好啊
有效存款
2.
纽约州纽约市第五大道350号,邮编:11223
40.595024898648262
-73.969506248831749
";
var doc=XDocument.Parse(xmlString);
XNS=”http://schemas.microsoft.com/search/local/ws/rest/v1";
变量位置=单据子体(ns+“点”)
.选择(p=>
新的{
纬度=(双)p元素(ns+“纬度”),
经度=(双)p.Element(ns+“经度”)
});
我通常使用以下命令:var latlong=from c in doc.subjections()。其中(x=>x.Name.LocalName==“Point”)
string xmlString =
@"
<Response xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schemas.microsoft.com/search/local/ws/rest/v1"">
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>2</EstimatedTotal>
<Resources>
<Location>
<Name>350 Avenue V, New York, NY 11223</Name>
<Point>
<Latitude>40.595024898648262</Latitude>
<Longitude>-73.969506248831749</Longitude>
</Point>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
";
var doc = XDocument.Parse(xmlString);
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
var positions = doc.Descendants(ns + "Point")
.Select(p =>
new {
Latitude = (double)p.Element(ns + "Latitude"),
Longitude = (double)p.Element(ns + "Longitude")
});