实际上我已经做了一些带有xml解析的windows phone 8应用程序,比如解析新闻rss feed并且工作正常。但是我的最新项目有问题。,
我有一个政治新闻rss feed(http://ibnlive.in.com//ibnrss//rss//india//india.xml)并尝试解析它并将其显示在列表中。
但是我无法解析它。我希望我的代码是正确的
这是我的代码c#
namespace indianPolitics
{
public partial class news1 : PhoneApplicationPage
{
ProgressIndicator _progressIndicator = new ProgressIndicator();
public news1()
{
InitializeComponent();
Loaded += news1_Loaded;
}
void news1_Loaded(object sender, RoutedEventArgs e)
{
WebClient _client = new WebClient();
_client.DownloadStringCompleted += _client_DownloadStringCompleted;
_client.DownloadStringAsync(new Uri("http://ibnlive.in.com//ibnrss//rss//india//india.xml", UriKind.Absolute));
SystemTray.ProgressIndicator = _progressIndicator;
_progressIndicator.IsIndeterminate = true;
_progressIndicator.IsVisible = true;
}
private void _client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Error Message"+e.Error.Message);
});
}
else
{
try
{
MessageBox.Show("Result Before Parse:"+e.Result);
XDocument xmlDoc = XDocument.Parse(e.Result);
var all = xmlDoc.Descendants("channel");
List<News1> _singleRsses = new List<News1>();
foreach (var item in all)
{
var itemVedio = item.Descendants("item");
foreach (var xElement in itemVedio)
{
string _title = xElement.Element("title").Value;
string _description = xElement.Element("description").Value;
string pubDate = xElement.Element("pubDate").Value;
string link = xElement.Element("link").Value;
News1 _singleRss = new News1
{
Title = _title,
Description = _description,
PubDate = pubDate,
Link = link
};
_singleRsses.Add(_singleRss);
}
}
singleListbox.DataContext = _singleRsses;
}
catch (Exception exp)
{
MessageBox.Show("Exception: "+exp);
_progressIndicator.IsIndeterminate = false;
_progressIndicator.IsVisible = false;
}
finally
{
_progressIndicator.IsIndeterminate = false;
_progressIndicator.IsVisible = false;
}
}
}
private void News1Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
}
}
public class News1
{
public string Title { get; set; }
public string ImageSource { get; set; }
public string Description { get; set; }
public string PubDate { get; set; }
public string Link { get; set; }
}
}
和我的XAML代码
<Grid x:Name="ContentPanel" Grid.RowSpan="2">
<Grid.Resources>
<DataTemplate x:Key="NewsDateTemplate">
<Grid Tag="{Binding Link}" x:Name="News1Grid" Tap="News1Grid_Tap" Width="420" Visibility="Visible" Margin="0,0,0,10" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding PubDate}" Foreground="LightGoldenrodYellow" FontSize="18"/>
<Border Background="#FFD69F0B" Grid.Row="1">
<TextBlock Text="{Binding Title}" Foreground="Black" TextWrapping="NoWrap" FontSize="20" FontWeight="Bold"/>
</Border>
<Grid Width="420" HorizontalAlignment="Left" Grid.Row="2" Margin="0,0,0,10">
<TextBlock Foreground="Black" Text="{Binding Description}" TextWrapping="Wrap" TextTrimming="WordEllipsis"/>
</Grid>
<Rectangle Fill="Black" Height="1" Grid.Row="3" Opacity=".5"/>
</Grid>
</DataTemplate>
</Grid.Resources>
<Border VerticalAlignment="Top" Height="80" Background="LightGray">
<TextBlock Name="ministerHeader" Text="Ministers" FontSize="40" TextTrimming="WordEllipsis" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" Foreground="Black" FontFamily="Times New Roman" ></TextBlock>
</Border>
<ListBox Name="singleListbox" Margin="0,85,0,0" ItemsSource="{Binding}" ItemTemplate="{StaticResource NewsDateTemplate}">
</ListBox>
</Grid>
注意:如果我像这样解析其他xml提要(http://www.skysports.com//rss//0,20514,11827,00.xml)意味着
它解析xml并在列表框中显示数据。
我还打印了
的结果 private void _client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
带
的事件处理程序 MessageBox.Show("Result Before Parse:"+e.Result);
,它显示“空”消息框。,
还有
catch (Exception exp)
{
MessageBox.Show("Exception: "+exp);
}
它显示如下错误:
"Exception: Root Element Missing"
请有人给我正确的解决方案。我有几周时间都在努力解决这个问题。
提前感谢。