2013-02-25 136 views
0

所以我有这个饲料,我缩小了不需要的节点。为什么这不会返回XML Feed的结果?

<ArrayOfService xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<Service z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"> 
<EntityKey z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
    <a:EntityContainerName>busExpressEntities</a:EntityContainerName> 
    <a:EntityKeyValues> 
    <a:EntityKeyMember> 
     <a:Key>serviceId</a:Key> 
     <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1149</a:Value> 
    </a:EntityKeyMember> 
    </a:EntityKeyValues> 
    <a:EntitySetName>ServiceSet</a:EntitySetName> 
</EntityKey> 
<client z:Id="i3"> 
    <EntityKey z:Id="i4" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
    <a:EntityContainerName>busExpressEntities</a:EntityContainerName> 
    <a:EntityKeyValues> 
     <a:EntityKeyMember> 
     <a:Key>clientId</a:Key> 
     <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema"></a:Value> 
     </a:EntityKeyMember> 
    </a:EntityKeyValues> 
    <a:EntitySetName>ClientSet</a:EntitySetName> 
    </EntityKey> 
    <OnlineOrdersEmailAddress></OnlineOrdersEmailAddress> 
    <SiriEndPointUrl>/DataBroker/</SiriEndPointUrl> 
    <clientId>brightonbuses</clientId> 
    <clientName>brightonbuses</clientName> 
    <clientRole /> 
    <customers /> 
    <emailDistributionList /> 
    <emailNewsletter /> 
    <emailTemplate /> 
    <facebookAccessToken>226218420727119|9fab190dcc99dc2750006a31.3-642660078|191977440844064|WaOZ-7z6_ih2OwHmerQ95uz0nFU</facebookAccessToken> 
    <facebookId>191977440844064</facebookId> 
    <fareMatrix /> 
    <newsCategory /> 
    <newsletter /> 
    <password /> 
    <pdfDirectoryPath>C:\buscms\wwwroot\operators\\data-files\pdfs</pdfDirectoryPath> 
    <pdfDirectoryUrl>http:///uploadedfiles</pdfDirectoryUrl> 
    <primaryEmailAddress>[email protected]</primaryEmailAddress> 
    <primaryWebAddress>http://www.buses.co.uk</primaryWebAddress> 
    <realtimeUrl>http://buses.citytransport.org.uk/smartinfo/service/jsp?stName=#gpsStopName#&amp;olifServerId=182&amp;autorefresh=0&amp;default_autorefresh=20&amp;routeId=182%2F#serviceName#&amp;stopId=#gpsStopName#&amp;optDir=#destination#&amp;nRows=10&amp;showArrivals=n&amp;optTime=now&amp;time=&amp;allLines=#allLines#</realtimeUrl> 
    <services> 
    <Service z:Ref="i1" /> 
    <Service z:Id="i5"> 
     <EntityKey z:Id="i6" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
     <a:EntityContainerName>busExpressEntities</a:EntityContainerName> 
     <a:EntityKeyValues> 
      <a:EntityKeyMember> 
      <a:Key>serviceId</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1150</a:Value> 
      </a:EntityKeyMember> 
     </a:EntityKeyValues> 
     <a:EntitySetName>ServiceSet</a:EntitySetName> 
     </EntityKey> 
     <client z:Ref="i3" /> 
     <clientReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
     <a:EntityKey z:Ref="i4" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
     </clientReference> 
     <journeyPreferredService /> 
     <newsArticles /> 
     <place /> 
     <primaryUrl>http://www.buses.co.uk/1A</primaryUrl> 
     <relativePosition>2</relativePosition> 
     <routes> 
     <Route z:Id="i7"> 
      <EntityKey z:Id="i8" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
      <a:EntityContainerName>busExpressEntities</a:EntityContainerName> 
      <a:EntityKeyValues> 
       <a:EntityKeyMember> 
       <a:Key>routeId</a:Key> 
       <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1236</a:Value> 
       </a:EntityKeyMember> 
      </a:EntityKeyValues> 
      <a:EntitySetName>RouteSet</a:EntitySetName> 
      </EntityKey> 
      <POI /> 
      <POILines /> 
      <dateEffectiveFrom i:nil="true" /> 
      <dateEffectiveTo i:nil="true" /> 
      <isLive>true</isLive> 
      <relativePosition>0</relativePosition> 
      <routeDescription /> 
      <routeId>1236</routeId> 
      <routeMap /> 
      <routeName>1A Mile Oak - Whitehawk</routeName> 
      <route_stop /> 
      <route_stop_stop /> 
      <route_stop_timetable_stop /> 
      <service z:Ref="i5" /> 
      <serviceReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
      <a:EntityKey z:Ref="i6" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
      </serviceReference> 
      <timetables /> 
      <validDestination>Whitehawk</validDestination> 
     </Route> 
     <Route z:Id="i9"> 
      <EntityKey z:Id="i10" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
      <a:EntityContainerName>busExpressEntities</a:EntityContainerName> 
      <a:EntityKeyValues> 
       <a:EntityKeyMember> 
       <a:Key>routeId</a:Key> 
       <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1232</a:Value> 
       </a:EntityKeyMember> 
      </a:EntityKeyValues> 
      <a:EntitySetName>RouteSet</a:EntitySetName> 
      </EntityKey> 
      <POI /> 
      <POILines /> 
      <dateEffectiveFrom i:nil="true" /> 
      <dateEffectiveTo i:nil="true" /> 
      <isLive>true</isLive> 
      <relativePosition>0</relativePosition> 
      <routeDescription /> 
      <routeId>1232</routeId> 
      <routeMap /> 
      <routeName>1A Whitehawk - Mile Oak</routeName> 
      <route_stop /> 
      <route_stop_stop /> 
      <route_stop_timetable_stop /> 
      <service z:Ref="i5" /> 
      <serviceReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
      <a:EntityKey z:Ref="i6" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
      </serviceReference> 
      <timetables /> 
      <validDestination>Mile Oak</validDestination> 
     </Route> 
     </routes> 
     <serviceAbbreviatedName /> 
     <serviceDescription>Whitehawk - County Hospital - City Centre - Hove - Portslade - Mile Oak</serviceDescription> 
     <serviceIconUrl>http://www.buscms.com/uploadedFiles/brightonbuses/service-icons/1A.png</serviceIconUrl> 
     <serviceId>1150</serviceId> 
     <serviceName>1A</serviceName> 
     <serviceNotes /> 
     <serviceText>Whitehawk - Mile Oak</serviceText> 
     <serviceUrls /> 
    </Service> 

该服务重复自身。

然后我有这样的代码来获取数据:

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
     if (e.Result != null) 
     { 
      XDocument doc = XDocument.Parse(e.Result); 
      XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary"; 
      var routeNames = (from n in doc.Descendants(ns + "ArrayOfService") 
          select new RootContainer 

          { 

           Stops = (from s in n.Elements(ns + "Service").Elements(ns + "client").Elements(ns + "services").Elements(ns + "Service").Elements(ns + "Service") 

              select new Stops 

              { 
               StopName = s.Element(ns + "serviceDescription").Value, 
               serviceIconUrl = s.Element(ns + "Service").Element(ns + "serviceIconUrl").Value, 
              // NaptanCode = s.Element(ns + "Service").Element(ns + "serviceIconUrl").Value, 

              // StopId = s.Element(ns + "stop").Element(ns + "stopId").Value 

              }).ToList() 
          }).Single(); 


      listBox1.ItemsSource = routeNames.Stops; 

而且它这样在Xmal位页装订:

<ListBox x:Name="listBox1" ScrollViewer.VerticalScrollBarVisibility="Disabled" Foreground="Red" Height="160" VerticalAlignment="Bottom" Margin="-10,0,10,377" Grid.Row="1"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <!-- <TextBlock Text="Bus Stops: "/>--> 
        <TextBlock Text="{Binding StopName}"/> 
        <!-- <TextBlock Text="Timetables: "/>--> 
         <TextBlock Text="{Binding serviceIconUrl}"/>      
        <!-- <TextBlock Text="{Binding StopId}"/>--> 
        <TextBlock Text=" "/> 
       </StackPanel> 
      </DataTemplate> 

     </ListBox.ItemTemplate> 
    </ListBox> 

为什么我没有得到任何回应?我知道这很简单,就是找不到它。

回答

1

您的LINQ查询在末尾包含太多Service元素,它不会找到它。

目前,它是:

n.Elements(ns + "Service") 
.Elements(ns + "client") 
.Elements(ns + "services") 
.Elements(ns + "Service") 
.Elements(ns + "Service") 

它应该是:

n.Elements(ns + "Service") 
.Elements(ns + "client") 
.Elements(ns + "services") 
.Elements(ns + "Service") 
+0

您好,感谢我已经做到了 - 但我仍然得到一个错误 - 型“System.NullReferenceException”未处理的异常发生在BrightonHoveBuses.dll – 2013-02-25 22:47:16

+0

@HenryEdwards - 这可能意味着您在'select'块中访问的内容(元素或属性)在XML文档中不存在 – 2013-02-26 01:22:30

相关问题