首页 > 代码库 > Silverlight 使用IsolatedStorage新建XML文件,并且用LINQ查询XML
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。