Silverlight 使用IsolatedStorage建立XML檔案,並且用LINQ查詢XML

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   檔案   

代碼Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;using System.Linq;using System.Windows.Shapes;using System.Collections.Generic;using System.Collections.ObjectModel;using System.IO.IsolatedStorage;using System.IO;using System.Xml.Linq;using System.Xml;namespace ServiceFacade{    public class FResourceCategoryCode    {       readonly string strFileName = "ResourceCategoryCode.xml";        List<FResourceCategoryCodeModel> lfrccs = new List<FResourceCategoryCodeModel>();/// <summary>/// 產生XML快取檔案/// </summary>public void CreateFile(List<FResourceCategoryCodeModel> lfrccm){try{using (IsolatedStorageFile isoStore =IsolatedStorageFile.GetUserStoreForApplication()){using (IsolatedStorageFileStream isoStream =new IsolatedStorageFileStream(strFileName,FileMode.Create, isoStore)){// 根據每個使用者儲存一個資訊XmlWriterSettings settings = new XmlWriterSettings();settings.Indent = true;// 建立一個XmlWriter.using (XmlWriter writer = XmlWriter.Create(isoStream, settings)){//設定XML的根writer.WriteStartElement("ResourceCategorys");foreach (var v in lfrccm){//設定每個元素的根writer.WriteStartElement("ResourceCategory");//以下為設定每個元素及值writer.WriteStartElement("cncCategroyStandard");writer.WriteString(v.CncCategroyStandard);writer.WriteEndElement();writer.WriteStartElement("cnvcCategoryCode");writer.WriteString(v.CnvcCategoryCode);writer.WriteEndElement();writer.WriteStartElement("cnvcCategoryName");writer.WriteString(v.CnvcCategoryName);writer.WriteEndElement();writer.WriteStartElement("cniLevel");writer.WriteString(Convert.ToString(v.CniLevel));writer.WriteEndElement();writer.WriteStartElement("cnvcParentCode");writer.WriteString(v.CnvcParentCode);writer.WriteEndElement();writer.WriteStartElement("cniOrderID");writer.WriteString(Convert.ToString(v.CniOrderID));writer.WriteEndElement();//XML結束標記writer.WriteEndElement();}//XML根結束標記writer.WriteEndElement();writer.Flush();}}}}catch{;}}/// <summary>        /// 讀取快取檔案        /// </summary>        private void ReadXml()        {            using (IsolatedStorageFile store =                     IsolatedStorageFile.GetUserStoreForApplication())            {                //讀取檔案並把檔案轉化為StreamReader                StreamReader reader = new StreamReader(store.OpenFile(strFileName,                   FileMode.Open, FileAccess.Read));                //把字串轉化為XML                XDocument xmlStory = XDocument.Parse(reader.ReadToEnd());                //利用LINQ TO XML轉化為集合               var varDetails = from details in xmlStory.Descendants("ResourceCategory")  //只取得節點為ResourceCategory的資訊                                 select new FResourceCategoryCodeModel                                 {                                     CncCategroyStandard = (string)details.Element("cncCategroyStandard"),                                     CnvcCategoryCode = (string)details.Element("cnvcCategoryCode"),                                     CnvcCategoryName = (string)details.Element("cnvcCategoryName"),                                     CniLevel = (int)details.Element("cniLevel"),                                     CniOrderID = (int)details.Element("cniOrderID"),                                     CnvcParentCode = (string)details.Element("cnvcParentCode")                                 };                lfrccs = varDetails.ToList<FResourceCategoryCodeModel>();                store.Dispose();            }        }/// <summary>        /// 實體類        /// </summary>  public class FResourceCategoryCodeModel    {        private int _cniCategroyID;        /// <summary>        /// 流水號        /// </summary>        public int CniCategroyID        {            get { return _cniCategroyID; }            set { _cniCategroyID = value; }        }        private string _cncCategroyStandard;        /// <summary>        /// 標準分類編碼        /// </summary>        public string CncCategroyStandard        {            get { return _cncCategroyStandard; }            set { _cncCategroyStandard = value; }        }        private string _cnvcCategoryCode;        /// <summary>        /// 資源分類編碼        /// </summary>        public string CnvcCategoryCode        {            get { return _cnvcCategoryCode; }            set { _cnvcCategoryCode = value; }        }        private string _cnvcCategoryName;        /// <summary>        /// 資源分類名稱        /// </summary>        public string CnvcCategoryName        {            get { return _cnvcCategoryName; }            set { _cnvcCategoryName = value; }        }        private int _cniLevel;        /// <summary>        /// 資源分類層級        /// </summary>        public int CniLevel        {            get { return _cniLevel; }            set { _cniLevel = value; }        }        private string _cnvcParentCode;        /// <summary>        /// 資源分類父級代碼        /// </summary>        public string CnvcParentCode        {            get { return _cnvcParentCode; }            set { _cnvcParentCode = value; }        }        private int _cniOrderID;        /// <summary>        /// 排序ID        /// </summary>        public int CniOrderID        {            get { return _cniOrderID; }            set { _cniOrderID = value; }        }        private string _cnvcRemark;        /// <summary>        /// 解說文字        /// </summary>        public string CnvcRemark        {            get { return _cnvcRemark; }            set { _cnvcRemark = value; }        }}}}

然後通過調用方法CreateFile和ReadXml就可以實現了,檔案存放的地址是SL的獨立空間,在XP系統下的存放地址為C:\Documents and Settings\使用者名稱\Local Settings\Application Data\Microsoft\Silverlight目錄下,可以找到很多快取檔案

文章轉自:http://www.cnblogs.com/888h/archive/2010/10/20/1856407.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.