zoukankan      html  css  js  c++  java
  • 使用IsolatedStorage新建XML文件,并且用LINQ查询XML

    今天实现了用IsolatedStorage新建XML文件,并且使用LINQ查询XML,虽然以前也用到了IsolatedStorage,但是当时没有保存为文件,而且直接使用的键/值,文件会自动创建。今天做的项目一个目录数,数据量特别大,每次形成TreeView时,时间比较长,另外这些数据基本上不会修改,所以想到把数据保存到本地,然后从本地读取数据,这样就会很快。下面我就分享一下我的代码

    代码
    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目录下,可以找到很多缓存文件
    好了,就写到这了,有时间再完善一下,希望对大家有用
  • 相关阅读:
    设计模式之工厂方法3
    Orchard CMS中如何打包不带源码的模块
    Dump Checking
    认识WinDbg
    Bootstrap3.0学习第九轮(CSS补充)
    SVN版本冲突解决详解
    windbg Symbol file path
    SVN下错误集锦
    MvcMovieStore mvc5.0,EF6.01
    SQL Server中的窗口函数
  • 原文地址:https://www.cnblogs.com/888h/p/1856407.html
Copyright © 2011-2022 走看看