【发布时间】:2012-06-24 00:23:23
【问题描述】:
我有一个通过 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();
}
我觉得我很接近弄明白了。
【问题讨论】:
-
给在 C# 中寻找 NextBus 的未来谷歌用户的说明:我在 github.com/jayhill/NextBus.NET 为 NextBus API 创建了一个 .NET 客户端。
标签: c# xml linq windows-phone-7 httpwebrequest