我有一个尝试从 Web 服务获取数据的 Windows Phone 7.1 应用程序。数据采用 XML 格式。语言是 Visual Studio 2010 下的 vb.net,带有 Async CTP 3。
我已成功从 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
不包含任何内容?如何使它包含正确的 XElement resultdoc.Descendants("movie")
?