文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在Silverlight 程序中读取服务器端的XML格式文件

在Silverlight 程序中读取服务器端的XML格式文件

时间:2011-04-29  来源:DotNet编程

1、读取并解析XML文档示例。

 

           //StringReader 对象从字符串中读取字符,在构造过程中取得一个字符串值。
            StringReader stream;
            stream = new StringReader("<?xml version='1.0'?>" +
                "<!-- This file represents a fragment of a book store inventory database. -->" +
                "<bookstore>" +
                " <book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\">" +
                "   <title>The Autobiography of Benjamin Franklin</title>" +
                "   <author>" +
                "       <first-name>Benjamin</first-name>" +
                "       <last-name>Franklin</last-name>" +
                "   </author>" +
                "   <price>8.99</price>" +
                " </book>" +
                " <book genre=\"novel\" publicationdate=\"1967\" ISBN=\"0-201-63361-2\">" +
                "   <title>The Confidence Man</title>" +
                "   <author>" +
                "       <first-name>Herman</first-name>" +
                "       <last-name>Melville</last-name>" +
                "   </author>" +
                "   <price>11.99</price>" +
                " </book>" +
                "  <book genre=\"philosophy\" publicationdate=\"1991\" ISBN=\"1-861001-57-6\">" +
                "   <title>The Gorgias</title>" +
                "   <author>" +
                "       <name>Plato</name>" +
                "   </author>" +
                "   <price>9.99</price>" +
                " </book>" +
                "</bookstore>");

            //StreamReader 对象用于从文件读取字符。在构造过程中,该对象读入要读取的文件的名称
            //System.IO.StreamReader stream = new System.IO.StreamReader("books.xml");
            //使用指定的流创建一个新的 XmlReader 实例。 
            using (XmlReader reader = XmlReader.Create(stream))
            {
                //XmlTextReader reader = null;
                //使用数据流构造一个 XmlTextReader 类。通常,如果需要在不使用文档对象模型 (DOM) 的情况下将 XML 作为要访问的原始数据,
                //则可以使用 XmlTextReader;因此,XmlTextReader 提供了一种更快读取 XML 的机制。XmlTextReader 使用不同的构造函数来
               指定 XML 数据的位置。以下代码从某个流加载 XmlTextReader:
                //reader = new XmlTextReader(stream);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:// The node is an Element.
                            Console.Write("<" + reader.Name);

                            while (reader.MoveToNextAttribute()) // Read attributes.
                                Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                            Console.Write(">");
                            Console.WriteLine(">");
                            break;
                        case XmlNodeType.Text://Display the text in each element.
                            Console.WriteLine(reader.Value);
                            break;
                        case XmlNodeType.EndElement://Display end of element.
                            Console.Write("</" + reader.Name);
                            Console.WriteLine(">");
                            break;
                    }
                }

            }

 

2、获取服务器端的文件

 

         /// <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;
        }

 

 3、完整的代码

 

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            Uri uri = new Uri(GetURL() + "/versionManage.xml", UriKind.Absolute);
            WebClient Appclient = new WebClient();
            Appclient.OpenReadAsync(uri);
            Appclient.OpenReadCompleted += new OpenReadCompletedEventHandler(Appclient_OpenReadCompleted);
        }

        /// <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;
        }

        void Appclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Stream stream = e.Result;
            using (XmlReader reader = XmlReader.Create(stream))
            {
                       
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:// The node is an Element.
                            Console.Write("<" + reader.Name);
                            while (reader.MoveToNextAttribute()) // Read attributes.
                                Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                            Console.Write(">");
                            Console.WriteLine(">");
                            break;
                        case XmlNodeType.Text://Display the text in each element.
                            Console.WriteLine(reader.Value);
                            break;
                        case XmlNodeType.EndElement://Display end of element.
                            Console.Write("</" + reader.Name);
                            Console.WriteLine(">");
                            break;
                    }
                }

            }

        }

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载