后代循环中的空XElement

时间:2012-04-04 17:00:24

标签: windows-phone-7 linq-to-xml async-ctp

我有一个Windows Phone 7.1应用程序,试图从Web服务获取数据。数据采用XML格式。语言是使用Async CTP 3在Visual Studio 2010下的vb.net。

我已成功从Web服务检索响应并将字符串解析为XDocument。但是在尝试通过循环遍历XML来填充视图模型时遇到问题,因为循环变量不包含任何内容(我希望它包含XElement):

Private Async Function GetMovies() As Task
    Dim client As New WebClient
    Dim address = (New Uri(New Uri(APIROOT), "movieschedule/movies")).ToString

    Dim resulttxt = Await client.DownloadStringTaskAsync(address)

    Dim resultdoc = XDocument.Parse(resulttxt)
    Dim resultlist As New Collections.ObjectModel.ObservableCollection(Of Movies)
    For Each movie In resultdoc.Descendants("movie")

        ' **the variable 'movie' contains nothing**

        Dim m As New Movies With {
            .Cast = movie.Attribute("cast").Value,
            ...
            .Title = movie.Attribute("title").Value
        }
        resultlist.Add(m)
    Next

    Movies = resultlist
End Function

来自Web服务的XML示例:

<movies>
  <movie code="MOV1299" title="Age of Heroes" is3D="0" genre="THRILLER" rating="D" cast="Sean Bean, Danny Dyer, Izabella Miko, James D'Arcy, Sebastian Street, William Houston" director="Adrian Vitoria" language="ENGLISH" subtitle="BAHASA INDONESIA" />
  <movie code="MOV1325" title="Fast and Furious 5" is3D="0" genre="ACTION" rating="D" cast="Vin Diesel, Paul Walker, Dwayne Johnson, Jordana Brewster, Tyrese Gibson, Elsa Pataky" director="Justin Lin" language="ENGLISH" subtitle="BAHASA INDONESIA" />
</movies>

我在创建新电影的线上设置了一个断点。我可以确认已填充变量resultdoc。我尝试在即时窗口中resultdoc.Descendants("movie").Count返回15. resultdoc.Descendants("movie").First.Attribute("code").Value正确返回“MOV1299”。

当我从断点继续时,我收到了这个错误:

System.NullReferenceException was unhandled
  Message=NullReferenceException
  StackTrace:
       at MyApp.MainPageVM.VB$StateMachine_2_GetMovies.MoveNext()
       at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass5.<OnCompletedInternal>b__1(Object state)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

为什么变量movie什么都不包含?如何使其包含来自resultdoc.Descendants("movie")的正确XElement?

1 个答案:

答案 0 :(得分:0)

[从评论中删除]

转到“异常”对话框(应位于调试菜单下)并选择在抛出NullReferenceException时中断。您将看到错误的确切位置。可能发生的事情是,您从服务器返回的XML节点之一没有标题或强制转换或其他属性,而您正在为此而死