Silverlight 使用IsolatedStorage新建XML文件,并且用LINQ查询XML

代码

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

Silverlight 使用IsolatedStorage新建XML文件,并且用LINQ查询XML,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。