2012-06-24 56 views
0

我有一个XML feed,我通过httpwebrequest获取,而且我在解析feed时遇到了问题,因为它不像我以前试过的那样。到目前为止,我有我已经存储在需要帮助解析Windows Phone中的XML提要

XDocument doc = XDocument.Parse(feedString); 

网址 http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=N

我知道,当我倾倒,所有在调试目的一个列表框,我把一切都在那里,我米只是有解析饲料问题:

<body copyright="All data copyright San Francisco Muni 2012."> 
<route tag="N" title="N-Judah" color="003399" oppositeColor="ffffff" latMin="37.7601699" latMax="37.7932299" lonMin="-122.5092" lonMax="-122.38798"> 
<stop tag="5240" title="King St & 4th St" lat="37.7760599" lon="-122.39436" stopId="15240"/> 
<stop tag="5237" title="King St & 2nd St" lat="37.7796199" lon="-122.38982" stopId="15237"/> 
<stop tag="7145" title="The Embarcadero & Brannan St" lat="37.7846299" lon="-122.38798" stopId="17145"/> 
<stop tag="4510" title="Embarcadero Folsom St" lat="37.7907499" lon="-122.3898399" stopId="14510"/> 
<stop tag="5629" title="Tunnel Entry Point Inbound Nea" lat="37.79279" lon="-122.39126" stopId="15629"/> 

等等等等

我想在每个停止标记每个属性存储到一个数组,但我完全被困在我甚至会开始的过程中。

感谢

更新: 我想我得到它,首先MSDN链接上工作,但这种只得到的第一行:

using (XmlReader reader = XmlReader.Create(new StringReader(feed))) 
     { 
      reader.ReadToFollowing("stop"); 
      reader.MoveToFirstAttribute(); 
      string tag = reader.Value; 


      reader.MoveToNextAttribute(); 
      string title = reader.Value; 

      reader.MoveToNextAttribute(); 
      string lat = reader.Value; 

      reader.MoveToNextAttribute(); 
      string lon = reader.Value; 


     } 

我怎么会通过每个循环停止与该代码以上?

感谢

编辑:#2

这个循环的工作,但一直显示停止的第一行属性:

using (XmlReader reader = XmlReader.Create(new StringReader(feed))) 
      { 
       reader.ReadToFollowing("stop"); 
       while (reader.MoveToNextAttribute()) 
       { 

       // Move the reader back to the element node. 



        //reader.ReadToFollowing("stop"); 
        reader.MoveToFirstAttribute(); 
        string tag = reader.Value; 
        MessageBox.Show(tag); 

        reader.MoveToNextAttribute(); 
        string title = reader.Value; 
        MessageBox.Show(title); 
        reader.MoveToNextAttribute(); 
        string lat = reader.Value; 
        MessageBox.Show(lat); 
        reader.MoveToNextAttribute(); 
        string lon = reader.Value; 
        MessageBox.Show(lon); 


       } 
       reader.MoveToElement(); 
      } 

我觉得我如此接近计算出来。

+0

你试过这个http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx –

+0

注意未来的谷歌在C#中寻找NextBus:我创建了一个.NET https://github.com/jayhill/NextBus.NET上的NextBus API客户端。 – Jay

回答

0

这里是你可以使用:

XmlReader xmlReader = XmlReader.Create("http://webservices.nextbus.com/service/publicXMLFeed? command=routeConfig&a=sf-muni&r=N"); 
List<string> aTitle= new List<string>(); 

// Add as many as attributes you have in your "stop" element 

while (xmlReader.Read()) 
{ 
//keep reading until we see your element 
if (xmlReader.Name.Equals("stop") && (xmlReader.NodeType == XmlNodeType.Element)) 
{ 
    string title = xmlReader.GetAttribute("title"); 
    aTitle.Add(title); 

    // Add code for all other attribute you would want to store in list. 
    } 
} 

最后调用列表,并根据索引,你可以得到所有的项目。

0

解析XML文件有很多种方法。这里有一个简单的方法:

 XDocument doc = XDocument.Load(feedString); 
     var stops = doc.Document.Descendants(XName.Get("route")); 
     // Loop over all stops in the XML 
     foreach (var stop in stops) 
     { 

     } 

我不知道什么叫“存储在每个停止标记成一个阵列中的每个属性”的意思,但我会定义一个类型:

class RouteStop 
{ // make setters private and init them in ctor to make it immutable 
    public string Tag {get; set;} //maybe int ? 
    public string Title {get; set;} 
    public double Latitude {get; set;} 
    public double Longitude {get; set;} 
    public int ID {get; set;} 
} 

然后定义RouteStop

的列表
List<RouteStop> routeStops = new List<RouteStop>(); 

,只是在foreach循环中创建对象

foreach (var stop in stops) 
{ 
    var tag = stop.Attribute("tag").Value; 
    var title = stop.Attribute("title").Value; 
    var long = double.Parse(stop.Attribute("lon").Value, 
          CultureInfo.InvariantCulture); 
    //etc 
    routeStops.add(new RouteStop() { Tag = tag } //and so on 
} 
+0

啊谢谢,这工作。但我认为你需要“停下来”而不是“路线”。 –

+0

Bleh,我收到与该代码抛出的异常。我说得太快了。 –

1

在Linq的帮助下,这是一个完整的解决方案。只要看看result包含

using (WebClient w = new WebClient()) 
{ 
    string xml = w.DownloadString("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=N"); 
    XDocument xDoc = XDocument.Parse(xml); 
    var result = xDoc.Descendants("stop") 
        .Select(n => new 
        { 
         Tag = (string)n.Attribute("tag"), 
         Title = (string)n.Attribute("title"), 
         Lat = (string)n.Attribute("lat"), 
         Lon = (string)n.Attribute("lon"), 
         StopId = (string)n.Attribute("stopId") 
        }) 
        .ToArray(); 
}