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);
            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))
            {
                xReader.ReadToFollowing("TestData");
                string TestData = xReader.ReadElementContentAsString();
                MessageBox.Show(TestData);
                xReader.ReadToNextSibling("SecData");
                string SecData = xReader.ReadElementContentAsString();
                MessageBox.Show(SecData);
            }
        }
        /// <summary>
        /// 获取当前SILVERLIGHT程序存放网络URL的前缀
        /// </summary>
        /// <returns>URL前缀</returns>
        public static string GetURL()
        {
            ScriptObject location = (HtmlPage.Window.GetProperty("location") as ScriptObject);
            object r = location.GetProperty("href");
            string URL = r.ToString().Substring(0, r.ToString().LastIndexOf('/')); //截取到当前SILVERLIGHT程序存放网络URL的前缀
            return URL;
        }
    }
}
