C# Windows Phone 7从web服务读取和解析XML数据

C# Windows Phone 7从web服务读取和解析XML数据,c#,xml,parsing,windows-phone-7,C#,Xml,Parsing,Windows Phone 7,我希望将这些XML值从web服务读取到TextBlock,但此代码会导致NullReferenceException。我确信web服务不是空的。我怎样才能解决这个问题 将注释导致异常的行,该行为: // NOTE: NullReferenceException happens here penalty = resultElements.Element("penalty").Value; 代码 namespace PhoneApp4 { public partial class pun

我希望将这些XML值从web服务读取到
TextBlock
,但此代码会导致
NullReferenceException
。我确信web服务不是空的。我怎样才能解决这个问题

将注释导致异常的行,该行为:

// NOTE: NullReferenceException happens here
penalty = resultElements.Element("penalty").Value;
代码

namespace PhoneApp4
{
    public partial class pun : PhoneApplicationPage
    {
        WebClient pu;

        public pun()
        {
            InitializeComponent();
            pu = new WebClient();

            string pp ="http://82.212.89.6:888/mob/resources/punishments/studentPunishments/427400078/2";
            pu.DownloadStringAsync(new Uri(pp));

            pu.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pun_DownloadStringCompleted);
            pu.DownloadProgressChanged += new DownloadProgressChangedEventHandler(pun_DownloadProgressChanged);
        }

        void pun_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (e.UserState as string == "mobiforge")
            {
                textBlock1.Text = e.BytesReceived.ToString() + " bytes received.";
            }
        }

        void pun_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                XElement resultElements = XElement.Parse(e.Result);

                // NOTE: NullReferenceException happens here
                penalty = resultElements.Element("penalty").Value;
                semester = resultElements.Element("semester").Value;

                pun1.Text = penalty;
                ps1.Text = semester;
           }

        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            MainPage tt = e.Content as MainPage;
            if (tt != null)
            {
                textBlock1.Text = tt.txtb1.Text;
            }
        }

        public string penalty { get; set; }
        public string semester { get; set; }
    }
}

这是基于REST的吗??如果是这样,请考虑使用RESTRAP。

< P>这是WS-REST的一个吗?如果是这样,请考虑使用RESTRAP。< /P> < P>对我来说是有效的。
penalty = resultElements.Element("studentPunishmentsTable").Element("penalty").Value;
semester = resultElements.Element("studentPunishmentsTable").Element("semester").Value;
它对我有用

penalty = resultElements.Element("studentPunishmentsTable").Element("penalty").Value;
semester = resultElements.Element("studentPunishmentsTable").Element("semester").Value;

哪些行导致NullReferenceException?什么是stacktrace?惩罚=resultElements.Element(“惩罚”).Value;学期=结果元素(“学期”)值;以下是异常发生情况,哪些行导致NullReferenceException?什么是stacktrace?惩罚=resultElements.Element(“惩罚”).Value;学期=结果元素(“学期”)值;以下是发生的异常情况