C# 需要在Windows Phone中解析XML源的帮助吗

C# 需要在Windows Phone中解析XML源的帮助吗,c#,xml,linq,windows-phone-7,httpwebrequest,C#,Xml,Linq,Windows Phone 7,Httpwebrequest,我有一个通过httpwebrequest检索到的XML提要,我在解析提要时遇到了问题,因为这与我过去尝试过的时候不同。到目前为止,我有网址 我把它储存在 XDocument doc = XDocument.Parse(feedString); 我知道,当我出于调试目的将所有内容都转储到列表框中时,我会将所有内容都放到列表框中,我只是在解析提要时遇到了问题: <body copyright="All data copyright San Francisco Muni 2012.">

我有一个通过httpwebrequest检索到的XML提要,我在解析提要时遇到了问题,因为这与我过去尝试过的时候不同。到目前为止,我有网址

我把它储存在

 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"/>
我如何使用上面的代码循环通过每个站点

谢谢

编辑:#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();
           }

我觉得我很快就能弄明白了。

解析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>();

下面是在Linq帮助下的完整解决方案。只需查看
结果
包含的内容

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

以下是您可以使用的:

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.
  }
}
XmlReader=XmlReader.Create(“http://webservices.nextbus.com/service/publicXMLFeed?   命令=routeConfig&a=sf muni&r=N“;
List aTitle=新列表();
//在“stop”元素中添加尽可能多的属性
while(xmlReader.Read())
{
//继续阅读,直到我们看到你的元素
if(xmlReader.Name.Equals(“stop”)&&(xmlReader.NodeType==XmlNodeType.Element))
{
字符串title=xmlReader.GetAttribute(“title”);
增加(标题);
//为要存储在列表中的所有其他属性添加代码。
}
}

最后调用列表,根据索引您可以获得所有项目。

您尝试过这个吗?给未来在C#中寻找NextBus的Google用户一个提示:我在为NextBus API创建了一个.NET客户端。啊,谢谢,成功了。但是我认为你需要的是“停止”而不是“路由”。哎呀,我在用代码抛出异常。我说得太快了。
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();
}
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.
  }
}