Silverlight Read External XML Loading Configuration – Use WebClient to Read XAP Package and XML File

I find this post from http://stephenchy520.blog.com/2011/07/26/silverlight-read-external-xml-loading-configuration-use-webclient-read-xml-from-xap-package/. It is very helpful so that I get permission from author and share it with you. And I have changed some sentences and hope it to be more readable.

How to use Webclient to read XAP package and XML file under it?

For reading XML file under XAP package, we need to put the XML file to the web category which loads XAP package. And then use URI method to read XML file under the URL.

At first, we need to use WebClient to download this XML file with XML asynchronously. Then, use XmlReaders to get data information of XML file.

There are three tips we need to know.

1. Get web address prefix when calling Silverlight application XAP package to get URL of this XML file.

            public static string GetURL()
            {
                ScriptObject location = (HtmlPage.Window.GetProperty(“location”) as ScriptObject);
                object r = location.GetProperty(“href”);

                //Intercept current URL prefix which is to save Silverlight application.
                string URL = r.ToString().Substring(0, r.ToString().LastIndexOf(‘/’));
                return URL;
            }

2. Download XML with WebClient

            Uri uri = new Uri(GetURL() + “/Config.xml”, UriKind.Absolute);

            //Use WebClient to download config.xml and read asynchronously.
            WebClient Appclient = new WebClient();
            Appclient.OpenReadAsync(uri);
            Appclient.OpenReadCompleted += new OpenReadCompletedEventHandler(Appclient_OpenReadCompleted);

3. Use XmlReader to read XML file.

            Stream stream = e.Result;
            using (XmlReader xReader = XmlReader.Create(stream))
            {
                //Use XmlReader to find “TestData” Elemenet and get its data.
                xReader.ReadToFollowing(“TestData”);

                string TestData = xReader.ReadElementContentAsString();
                MessageBox.Show(TestData);

                //Use ReadToNextSibling to find “SecData” Element data until the data is found.
                xReader.ReadToNextSibling(“SecData”);

                string SecData = xReader.ReadElementContentAsString();
                MessageBox.Show(SecData);
            }

Add the following namespace as reference:

using System.IO;
using System.Xml;
using System.Windows.Browser;

Code in details as following:

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.IO;
using System.Xml;
using System.Windows.Browser;

namespace SLReadXML
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Uri uri = new Uri(GetURL() + “/Config.xml”, UriKind.Absolute);
            //Use WebClient to download config.xml and read asynchronously.
            WebClient Appclient = new WebClient();

            Appclient.OpenReadAsync(uri);
            Appclient.OpenReadCompleted += new OpenReadCompletedEventHandler(Appclient_OpenReadCompleted);
        }
        void Appclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Stream stream = e.Result;
            using (XmlReader xReader = XmlReader.Create(stream))
            {
                //Use XmlReader to find “TestData” Elemenet and get its data.
                xReader.ReadToFollowing(“TestData”);

                string TestData = xReader.ReadElementContentAsString();
                MessageBox.Show(TestData);
                //Use ReadToNextSibling to find “SecData” Element data until the data is found.
                xReader.ReadToNextSibling(“SecData”);

                string SecData = xReader.ReadElementContentAsString();
                MessageBox.Show(SecData);
           }
        }

        ///<summary>
        ///Get current URL prefix which is to save Silverlight application.
        ///</summary>
        ///<returns>URL Prefix</returns>
        public static string GetURL()
        {
            ScriptObject location = (HtmlPage.Window.GetProperty(“location”) as ScriptObject);
            object r = location.GetProperty(“href”);
            //Intercept current URL prefix which is to save Silverlight application.
            string URL = r.ToString().Substring(0, r.ToString().LastIndexOf(‘/’));

            return URL;
        }
    }
}

Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “Silverlight Read External XML Loading Configuration – Use WebClient to Read XAP Package and XML File”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s