Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在xml中选择内部节点值时ArgumentNullException_C#_Xml_Windows Phone 7_Bing Maps - Fatal编程技术网

C# 在xml中选择内部节点值时ArgumentNullException

C# 在xml中选择内部节点值时ArgumentNullException,c#,xml,windows-phone-7,bing-maps,C#,Xml,Windows Phone 7,Bing Maps,我试图从下面给出的xml文档中获取位置名称值,但它显示argumentnullexception。任何帮助都将不胜感激 <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"> <

我试图从下面给出的xml文档中获取位置名称值,但它显示argumentnullexception。任何帮助都将不胜感激

    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
  <Copyright>
Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.
  </Copyright>
  <BrandLogoUri>
http://dev.virtualearth.net/Branding/logo_powered_by.png
  </BrandLogoUri>
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
  <TraceId>
dd31ffaf098f4406b7ecdd0da36680ff
  </TraceId>
      <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>1 Microsoft Way, Redmond, WA 98052</Name>
          <Point>
            <Latitude>47.640568390488625</Latitude>
            <Longitude>-122.1293731033802</Longitude>
          </Point>....

问题是,您要取回一个集合(它只有一个Location对象),然后不使用它

试试这个:

Location loc = result.Descendants(ns1 + "Location")
                     .Select(x => new Location { address = (string)x.Element(ns1 + "Name") })
                     .First(); // This gets you a single item
MessageBox.Show(loc.address);
或者,如果可能有多个返回,则可以迭代:

var locs = result.Descendants(ns1 + "Location")
                     .Select(x => new Location { address = (string)x.Element(ns1 + "Name") });
foreach(var loc in locs)
    MessageBox.Show(loc.address);

(我确认这在我刚刚运行时有效-如果这对您不起作用,请确切说明您遇到的问题)。

我没有发现您的代码有任何问题,除了以下几点:

Location loc=新位置();
MessageBox.Show(地址);
您正在创建一个新对象并试图显示
loc.address
?当然,您将获得
ArgumentNullException

address1将已经保存xml的结果,但您正在创建一个新对象

试试这个:

Location loc = result.Descendants(ns1 + "Location")
                     .Select(x => new Location { address = (string)x.Element(ns1 + "Name") })
                     .First(); // This gets you a single item
MessageBox.Show(loc.address);
首先删除此行:

Location loc=新位置();
然后添加以下内容:

foreach(地址1中的变量地址){
MessageBox.Show(address.address);
}
p、 美国。
下次,在问题中显示哪一行抛出异常很重要,这样人们就可以看到问题所在。

同样的异常也会发生…:(哪一行引发异常?
e.Result
是否具有正确的非空值?我检查了解析e.Result后,整个xml文档是否存储在
Result
中。
messagebox.show
引发
System.Windows.ni.dll中发生了“System.ArgumentNullException”类型的异常,但未在中处理。)用户代码
变量
ns
具有值
{http://schemas.microsoft.com/search/local/ws/rest/v1}
感谢您的帮助@Alaa Masoud