请各位老师同仁帮忙看一下代码
本帖最后由 yyy8343 于 2014-01-01 18:32:35 编辑 代码如下:
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了
[解决办法]
要求16进制,但你输入的是 .
所以无效。