none
How to download an xml file in Silverlight application by using Visual Studio IDE and .NET 4.0 with C#?

    Question

  • Hello, please tell me how to Load an XML file either from project folder or from internet in a Silverlight application?

    I have tried the following approaches:

    1) I have followed Kirupa's tutorial on downloading an xml file, but it does not work.

    I am using Visual Studio 2010 and .NET 4 with C#.

    The tutorial that I followed is at: http://www.kirupa.com/blend_silverlight/loading_xml_sl2_pg1.htm

    I cannot see the downloaded content of the xml file in the messagebox or in the HtmlPage.Window.Alert(xmlData);

    2) I have also tried XDocument doc = XDocument.Load("xmlFile.xml");

    MessageBox.Show(doc.ToString());

    But the MessageBox.Show(doc.ToString()); does not appear. Please tell me what I should do so that I can download an xml file as a string in my silverlight project so that I can manipulate it to display the result in my silverlight application. This question comes after my previous question as I have realized that I might have to download the xml file for processing as the xml file is complicated and the images are not part of itemCollection; however, I am not sure.

    Please suggest what I should do so that I can download an xml file in silverlight .net 4.0 (VS 2010 IDE with C#) as a string so that I can use LINQ as I believe that is the way to go, but the tutorial I have found, from Kirupa, does not work ( the HtmlPage.Window.Alert(xmlData); or MessageBox.Show(...) does not appear).


    Please give your suggestions.

    Saturday, July 17, 2010 3:19 PM

Answers

All replies

  • Hi,

    Have a look at this post:

    http://forums.silverlight.net/forums/p/59835/151682.aspx

    HTH 

    Saturday, July 17, 2010 3:26 PM
  • Is there a way I can see the downloaded result as a string in MessageBox after WebClient finish downloading the xml file because that is I am trying to do. The tutorial is for a zip file, and mine is an xml file.


    Please note that I am using VS 2010 with .NEt 4.0 and C#, and it is for a silverlight application. All I have been trying to do is to see the string result after the content gets downloaded in my project eith by XDocument or WebClient, but nothing is working: means I cannot see the string in the MEssageBox.Show(...).


    Even Kirupa's tutorial is not working as I cannot see the downloaded string result in the message box.

    Saturday, July 17, 2010 3:40 PM
  • What do you get when you do this?

    WebClient xmlClient = new WebClient();
                xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
                xmlClient.DownloadStringAsync(new Uri("http://localhost:62134/MyXmlFile.xml", UriKind.RelativeOrAbsolute));
    
            
            void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    Messabox.Show(e.Result);
                }
            }


     

    Saturday, July 17, 2010 3:53 PM
  • I cannot see the result.


    I have created a new project and the xmlfile is inside the project folder.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication6
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                WebClient xmlClient = new WebClient();
                MessageBox.Show("Going To XMLFileLoaded");
                xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
                xmlClient.DownloadStringAsync(new Uri("XmlFile.xml", UriKind.RelativeOrAbsolute));

            }
            void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
            {
                MessageBox.Show("In XMLFileLoaded");
                MessageBox.Show("In XMLFileLoaded"+e.Result);
                MessageBox.Show("In XMLFileLoaded" + e.ToString());
                if (e.Error == null)
                {
                   MessageBox.Show(e.Result);
                }
                else if (e.Result != null)
                {
                    MessageBox.Show(e.Result);

                }
                else
                {
                    MessageBox.Show(e.Result);
                }
            }

        }
    }


    I can see the first two messagebox but  no message box containing any result...

    Saturday, July 17, 2010 5:25 PM
  • Now I can load a local xml file that is in my project folder by

    doing as below:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Xml.Linq;

    namespace SilverlightApplication6
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                InitializeComponent();
                XDocument feedXML;
                feedXML = XDocument.Load("xmlFile.xml");
                MessageBox.Show(feedXML.ToString());
            }
        }
    }


    I can see the xml file's content in the message box; however I cannot load the remote xml file. I need to use the webclient class, but as I said that I cannot see the resultant string in the message box....

    Saturday, July 17, 2010 5:36 PM
  • You removed the http://localhost from your uri.

    When you run your application take note of the address it will be something like http://localhost:56754 you need to prefix your Xmlfile with that.

    For instance change your XmlFile.xml to Default.html and you will see the contents of the html file that is in the root of your Web folder.

    Please note that this file needs to be in your Web project and not your Silverlight project 

    Saturday, July 17, 2010 6:46 PM
  • Hello,

    i will be much busy from this second till monday morning. Please check my situation after 21 hours(usa time: sunday morning) or after 40 hours, monday morning usa time.

    by tomorrow morning 12 pm pst, i hope to if i can, i will tell you my response. thank you a lot for participating, and please check back tomorrow and monday, if you can.

    Saturday, July 17, 2010 7:08 PM
  • Hello,

    I followed your advise and loaded the xml file in my root directory port:80, which is at port:80, but I was not able to load the xml file.

    However, now I can now load an xml file from my local machine either by using XDocument.Load or by using WebClient class.


    http://blog.kirupa.com/?p=227<----This blog helped me to load a local xml file by using webclient class in a silverlight project.


    However, I still cannot load an external xml file. Do you know how to load an external xml file in a silverlight 4 application that is hosted in a website?

    For example, how to load
    http://www.kirupa.com/net/files/sampleXML.xml <----

    in my local machine or in my personal server so that I can manipulate the xml file to show contents.


    Please note that if you answer, I will reply back after 24 hours, but please help or suggest in my situation.


     

    Sunday, July 18, 2010 3:41 PM
  • Replacing the above code as below will work as long as you can browse the file from your browser

    new Uri("http://www.kirupa.com/net/files/sampleXML.xml", UriKind.RelativeOrAbsolute)); 


     

    Sunday, July 18, 2010 4:09 PM
  • Hello,

    I have just solved the problem in hand, which is how to load an external xml file either to my local machine or in my
    web server.

    This website helped me to accomplish my work--->http://www.tanguay.info/web/index.php?pg=codeExamples&id=205

    And the code is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Xml;
    using System.Xml.Linq;

    namespace SilverlightApplication8
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent(); InitializeComponent();
                WebClient proxy = new WebClient();
                proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
                proxy.DownloadProgressChanged += new DownloadProgressChangedEventHandler(proxy_DownloadProgressChanged);
                proxy.DownloadStringAsync(new Uri("http://weather.yahooapis.com/forecastrss?p=USCA0470&u=f"));
            }

           

            void proxy_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                //ProgressIndicator.Text = e.ProgressPercentage + "% loaded.";
              //  ProgressIndicator.Text = "50% loaded.";
            }

            void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                XDocument xdoc = XDocument.Parse(e.Result);
                MessageBox.Show("HELLO");
                TheContent.Text = "";

                var quotes = from q in xdoc.Descendants("channel")
                             select new Quote
                             {
                                 Body = q.Element("title").Value
                             };

                foreach (Quote quote in quotes)
                {
                    TheContent.Text += quote.Body + "\n";
                    MessageBox.Show(quote.Body.ToString());
                }

               // ProgressIndicator.Text = "100% loaded.";
            }
        }

        public class Quote
        {
            public string Body { get; set; }
        }
    }


    The problem is solved; now I just have to get my data to parse and format them as I like.

    Thank you all for the participation. I am going to keep this thread open for 24 hours in case you have some comment; otherwise, I will close this thread after 24 hours.
    Please note that I will reply after 24 hours.

    Sunday, July 18, 2010 4:23 PM
  • I am new in silverlight. I am making application of portal. And in that application i want to allow user to download files.

    When user click on download button link the download box want to be show and ask for save,open,cancel.

    The file types are .jpg,.png,.zip. Please can u help me

    Monday, May 07, 2012 5:50 AM