Reputation: 19
I want these XML values read from the web service to a TextBlock
, but this code causes a NullReferenceException
. I'm sure from that the web service is not null. How can I solve this problem???
The line that causes the exception is commented and is:
// NOTE: NullReferenceException happens here
penalty = resultElements.Element("penalty").Value;
Code
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; }
}
}
Upvotes: 0
Views: 2503
Reputation: 169
It works for me
penalty = resultElements.Element("studentPunishmentsTable").Element("penalty").Value;
semester = resultElements.Element("studentPunishmentsTable").Element("semester").Value;
Upvotes: 3
Reputation: 632
Is this WS a REST based one?? If so consider using RestSharp.
Upvotes: 0