请各位老师同仁帮忙看一下代码

糊涂娃8343 糊涂娃8343 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
public partial class MainPage : PhoneApplicationPage
{
HttpWebRequest request = null;
HttpWebResponse response = null;
// 构造函数
public MainPage()
{
InitializeComponent();
string uri="http://fpad.ce.cn/hg/news_list.xml";

request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(GetResponseCallBack), null);
}

private void GetResponseCallBack(IAsyncResult ar)
{
List<News> newList = new List<Finace.News>();

response = (HttpWebResponse)request.EndGetResponse(ar);
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream,new Gb2312Encoding());

using (XmlReader xmlReader = XmlReader.Create(reader))
{
string title = "";
string intro = "";

while (xmlReader.Read())
{
try
{
if (xmlReader.ReadToFollowing("a"))
{
title = xmlReader.ReadElementContentAsString();
}
if (xmlReader.ReadToFollowing("summary"))
{
intro = xmlReader.ReadElementContentAsString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

if (title != "" || intro != "")
{
newList.Add(new News { Title = title, Intro = intro });
}
}

}
}
Dispatcher.BeginInvoke(() =>
{
NewsList.ItemsSource = newList;
});
}
}

class News
{
public string Title { set; get; }
public string Intro { set; get; }
}

不管标题内容是什么,每次单步调试到第三条时就报错,就是说xml中的前两条数据都能读取成功,到第三条if (xmlReader.ReadToFollowing("a"))就被catch了
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部