所以我有这个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>info@buses.co.uk</primaryEmailAddress>
<primaryWebAddress>http://www.buses.co.uk</primaryWebAddress>
<realtimeUrl>http://buses.citytransport.org.uk/smartinfo/service/jsp?stName=#gpsStopName#&olifServerId=182&autorefresh=0&default_autorefresh=20&routeId=182%2F#serviceName#&stopId=#gpsStopName#&optDir=#destination#&nRows=10&showArrivals=n&optTime=now&time=&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>
为什么我没有回复?我知道这很简单,就是找不到它。
答案 0 :(得分: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")