Home > DeveloperSection > Forums > Writing online XML file to textbox
Chintoo Semi

Total Post:135

Points:947
Posted on    April-01-2014 2:14 AM

 C# C# 
Ratings:


 1 Reply(s)
 887  View(s)
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);

            }

        }

    }

}



Pravesh Singh

Total Post:411

Points:2881
Posted on    April-10-2014 11:57 PM

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;


Don't want to miss updates? Please click the below button!

Follow MindStick