我如何从这个XML中获得所有的优势? Windows手机

时间:2022-10-31 11:39:33

I have a feed and this is the XML that I get from the feed : All i am intrested in is the routename ( and and the content within the nodes

我有一个feed,这是我从feed获得的XML:我感兴趣的是routename(以及节点内的内容)

<Route z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
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>routeId</a:Key>
    <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1231</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>1231</routeId>
<routeMap />
<routeName>1 Whitehawk - Mile Oak</routeName>
<route_stop>
<route_stop 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>routeId</a:Key>
        <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1231</a:Value>
      </a:EntityKeyMember>
      <a:EntityKeyMember>
        <a:Key>stopId</a:Key>
        <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">6882</a:Value>
      </a:EntityKeyMember>
    </a:EntityKeyValues>
    <a:EntitySetName>route_stop</a:EntitySetName>
  </EntityKey>
  <alightingTariffCode>1</alightingTariffCode>
  <boardingTariffCode>1</boardingTariffCode>
  <route z:Ref="i1" />
  <routeId>1231</routeId>
  <routeIndex>1</routeIndex>
  <routeReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
    <a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" />
  </routeReference>
  <stop 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>stopId</a:Key>
          <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">6882</a:Value>
        </a:EntityKeyMember>
      </a:EntityKeyValues>
      <a:EntitySetName>StopSet</a:EntitySetName>
    </EntityKey>
    <AtcoCode>149000006612</AtcoCode>
    <GPSStopName>Swanborough Dr.</GPSStopName>
    <Lat>50.8326729229821</Lat>
    <Long>-0.103217996656901</Long>
    <LongName>Swanborough Drive</LongName>
    <NaptanCode>bridmpj</NaptanCode>
    <OperatorsCode1>06612</OperatorsCode1>
    <OperatorsCode2>6612</OperatorsCode2>
    <OperatorsCode3>51581</OperatorsCode3>
    <OperatorsCode4>bridmpj</OperatorsCode4>
    <StopName>Swanborough Drive</StopName>
    <route_stop>
      <route_stop z:Ref="i3" />
    </route_stop>
    <route_stop_stop />
    <route_stop_timetable_stop />
    <stopId>6882</stopId>
  </stop>
  <stopId>6882</stopId>
  <stopReference 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" />
  </stopReference>
</route_stop>
<route_stop 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">1231</a:Value>
      </a:EntityKeyMember>
      <a:EntityKeyMember>
        <a:Key>stopId</a:Key>
        <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">6883</a:Value>
      </a:EntityKeyMember>
    </a:EntityKeyValues>
    <a:EntitySetName>route_stop</a:EntitySetName>
  </EntityKey>
  <alightingTariffCode>1</alightingTariffCode>
  <boardingTariffCode>1</boardingTariffCode>
  <route z:Ref="i1" />
  <routeId>1231</routeId>
  <routeIndex>2</routeIndex>
  <routeReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
    <a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" />
  </routeReference>
  <stop 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>stopId</a:Key>
          <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">6883</a:Value>
        </a:EntityKeyMember>
      </a:EntityKeyValues>
      <a:EntitySetName>StopSet</a:EntitySetName>
    </EntityKey>
    <AtcoCode>149000006613</AtcoCode>
    <GPSStopName>Haybourne Road</GPSStopName>
    <Lat>50.8317788816436</Lat>
    <Long>-0.10486427645364671</Long>
    <LongName>Haybourne Road</LongName>
    <NaptanCode>brimapm</NaptanCode>
    <OperatorsCode1>06613</OperatorsCode1>
    <OperatorsCode2>6613</OperatorsCode2>
    <OperatorsCode3>51701</OperatorsCode3>
    <OperatorsCode4>brimapm</OperatorsCode4>
    <StopName>Haybourne Road</StopName>
    <route_stop>
      <route_stop z:Ref="i7" />
    </route_stop>
    <route_stop_stop />
    <route_stop_timetable_stop />
    <stopId>6883</stopId>
  </stop>
  <stopId>6883</stopId>
  <stopReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
    <a:EntityKey z:Ref="i10" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" />
  </stopReference>
</route_stop>
</Route>

I have the following code to get the URL -

我有以下代码来获取URL -

 WebClient client = new WebClient(); 
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    Uri url = new Uri("http://www.buscms.com/brightonbuses/v1/route.aspx?routeid=1231&stops=true&maps=true", UriKind.Absolute); 
    client.OpenReadAsync(url); 

What's the next stop in this part

这部分的下一站是什么?

public void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {

    } 

So that I am able to get the stops in the xml that is in the url?

这样我就可以在URL中的xml中获取停靠点了吗?

1 个解决方案

#1


4  

For a start, I wouldn't use the OpenRead method, I would go with DownloadString instead, so your initial call would be:

首先,我不会使用OpenRead方法,而是使用DownloadString,因此您的初始调用将是:

        void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        Uri url = new Uri("http://www.buscms.com/brightonbuses/v1/route.aspx?routeid=1231&stops=true&maps=true", UriKind.Absolute);
        client.DownloadStringAsync(url);
    }

And your event handler would then look like this

然后你的事件处理程序就像这样

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 + "Route")
                                 select new
                                 {
                                     RouteName = n.Element(ns + "routeName").Value,
                                     Stops = (from s in n.Element(ns + "route_stop").Elements(ns + "route_stop")
                                              select new
                                              {
                                                  StopCode = s.Element(ns + "stop").Element(ns + "OperatorsCode4").Value,
                                                  StopName = s.Element(ns + "stop").Element(ns + "StopName").Value
                                                  StopId = s.Element(ns + "stop").Element(ns + "stopId").Value

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


                listBusStops.ItemsSource = routeNames.Stops;

                // Do something with the list of Route Names in routeNames
            }
        }
    }

Note the use of the XNamespace in order to find the routeName tags.

请注意使用XNamespace来查找routeName标记。

Don't forget to add a reference to System.Xml.Linq to your project.

不要忘记将System.Xml.Linq的引用添加到项目中。

#1


4  

For a start, I wouldn't use the OpenRead method, I would go with DownloadString instead, so your initial call would be:

首先,我不会使用OpenRead方法,而是使用DownloadString,因此您的初始调用将是:

        void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        Uri url = new Uri("http://www.buscms.com/brightonbuses/v1/route.aspx?routeid=1231&stops=true&maps=true", UriKind.Absolute);
        client.DownloadStringAsync(url);
    }

And your event handler would then look like this

然后你的事件处理程序就像这样

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 + "Route")
                                 select new
                                 {
                                     RouteName = n.Element(ns + "routeName").Value,
                                     Stops = (from s in n.Element(ns + "route_stop").Elements(ns + "route_stop")
                                              select new
                                              {
                                                  StopCode = s.Element(ns + "stop").Element(ns + "OperatorsCode4").Value,
                                                  StopName = s.Element(ns + "stop").Element(ns + "StopName").Value
                                                  StopId = s.Element(ns + "stop").Element(ns + "stopId").Value

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


                listBusStops.ItemsSource = routeNames.Stops;

                // Do something with the list of Route Names in routeNames
            }
        }
    }

Note the use of the XNamespace in order to find the routeName tags.

请注意使用XNamespace来查找routeName标记。

Don't forget to add a reference to System.Xml.Linq to your project.

不要忘记将System.Xml.Linq的引用添加到项目中。