Writing online XML file to textbox

Total Post:135

Points:947
C# 
C#
 1466  View(s)
Ratings:
Rate this:

I am using a PHP script to generate xml files. I want to write the data in the XML file to a Textblock in my Windows Phone 8 App. When I debug, I get an error which is not caught my the catch

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;
namespace xml1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create("http://cocktailpws.net23.net/requests/get_cocktail.php?id=10") as HttpWebRequest;
                request.BeginGetResponse(r =>
                {
                    var reponse = request.EndGetResponse(r);
                    //XDocument xmlDoc = XDocument.Load(reponse.GetResponseStream());
                    XmlReader reader = XmlReader.Create(reponse.GetResponseStream());
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element: // Het knooppunt is een element.
                                Console.Write("<" + reader.Name);
                                Console.WriteLine(">");
                                break;
                            case XmlNodeType.Text: //De tekst in elk element weergeven.
                                tb1.Text = tb1.Text + reader.Value + "\r\n";
                                Console.WriteLine(reader.Value);
                                break;
                            case XmlNodeType.EndElement: //Het einde van het element weergeven.
                                Console.Write("</" + reader.Name);
                                Console.WriteLine(">");
                                break;
                        }
                    }
                }, null);
            }
            catch (Exception myExc)
            {
                Console.WriteLine(myExc.Message);
            }
        }
    }
}

  1. Post:412

    Points:2888
    Re: Writing online XML file to textbox

    Hi Chintoo,

    I believe you must access tb1 from the UI thread, so I would suggest trying to use a statement similar to this:

    case XmlNodeType.Text: //De tekst in elk element weergeven.
    {
        tb1.Dispatcher.BeginInvoke(() =>
        {
            tb1.Text = tb1.Text + reader.Value + "\r\n";
        });
        Console.WriteLine(reader.Value);
    }
    break;

      Modified On Apr-04-2018 05:07:22 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!