0

我有一个通过 httpwebrequest 检索到的 XML 提要,我在解析提要时遇到问题,因为它与我过去尝试过的时间不同。到目前为止,我有网址 http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=N

我存储在其中

 XDocument doc = XDocument.Parse(feedString);

而且我知道,当我出于调试目的将所有内容都转储到列表框中时,我得到了所有内容,我只是在解析提要时遇到问题:

<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();
           }

我觉得我离弄清楚它很近了。

4

3 回答 3

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();
}
于 2012-06-24T00:48:20.393 回答
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
}
于 2012-06-24T00:41:00.127 回答
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.
  }
}

最后调用列表并根据索引获取所有项目。

于 2012-06-24T00:53:55.133 回答