//记得添加xml xml.linq的引用哦using System;using System.IO;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.Ink;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using System.Collections.Generic;using System.IO.IsolatedStorage;using System.Xml;using System.Xml.linq;namespace XmlMemoryDemo{ public class MemoryOper { private static string strXml = @"<?xml version=""1.0"" enCoding=""utf-8""?> <projects> <name>value</name> </projects>"; /// <summary> /// 判断文件是否存在 /// </summary> private static bool existFille(string path) { using (var isofile = IsolatedStoragefile.GetUserStoreForApplication()) { if (isofile.fileExists(path)) { return true; } return false; } } /// <summary> /// 判断节点是否存在 /// </summary> private static bool exitsNode(string path,string name,string value) { List<MemoryNode> nodes = getNodes(path); MemoryNode node = new MemoryNode(name,path); if (nodes.Contains(node)) { return true; } return false; } /// <summary> /// 创建文件 /// </summary> /// <param name="path"></param> private static voID createfile(string path) { if (!existFille(path)) { using (var isofile = IsolatedStoragefile.GetUserStoreForApplication()) { using (var stream = new IsolatedStoragefileStream(path,fileMode.Create,isofile)) { using (var writer = new StreamWriter(stream)) { writer.Writeline(strXml); writer.Close(); } stream.Close(); } } } } /// <summary> /// 返回所有节点 /// </summary> private static List<MemoryNode> getNodes(string path) { createfile(path); using (var isofile = IsolatedStoragefile.GetUserStoreForApplication()) { using (var stream = new IsolatedStoragefileStream(path,fileMode.Open,isofile)) { XmlReader reader = XmlReader.Create(stream); Xdocument xDoc = Xdocument.Load(reader); XElement Root = xDoc.Root; List<MemoryNode> results = new List<MemoryNode>(); foreach (XElement ele in Root.Elements()) { MemoryNode result = new MemoryNode(ele.name.ToString(),ele.Value.ToString()); results.Add(result); } stream.Close(); return results; } } } /// <summary> /// 通过名称获得value /// </summary> public static string getValue(string path,string name) { List<MemoryNode> nodes = getNodes(path); foreach(MemoryNode node in nodes) { if (node.name.Equals(name)) { return node.value; } } return ""; } /// <summary> /// 添加节点 /// </summary> public static voID addNode(string path,string value) { createfile(path); if (exitsNode(path,name,value)) { updateNode(path,value);//已经存在的节点更新即可 return; } using (var isofile = IsolatedStoragefile.GetUserStoreForApplication()) { Xdocument xdoc=null; using (IsolatedStoragefileStream isoStream = new IsolatedStoragefileStream(path,isofile)) { XElement newElement = new XElement(name,value); xdoc=Xdocument.Load(isoStream); xdoc.Root.Add(newElement); } using (IsolatedStoragefileStream isoStream = new IsolatedStoragefileStream(path,fileMode.Truncate,fileAccess.Write,isofile)) { using (StreamWriter sw = new StreamWriter(isoStream,System.Text.EnCoding.UTF8)) { xdoc.Save(sw); } } } } /// <summary> /// 更新节点 /// </summary> private static voID updateNode(string path,string value) { createfile(path); using (var isofile = IsolatedStoragefile.GetUserStoreForApplication()) { Xdocument xdoc = null; using (IsolatedStoragefileStream isoStream = new IsolatedStoragefileStream(path,value); xdoc = Xdocument.Load(isoStream); XElement root = xdoc.Root; foreach (XElement ele in root.Elements()) { if (ele.name.ToString().Equals(name)) { ele.Remove(); } } xdoc.Root.Add(new XElement(name,value)); /* foreach (XElement ele in root.Elements()) { if (ele.name.ToString().Equals(name)) { ele.Remove(); } } xdoc.Root.Add(new XElement(name,value)); */ /* foreach (XElement ele in root.Elements()) { if (ele.name.ToString().Equals(name)) { ele.ReplaceWith(new XElement(name,value)); } } */ } using (IsolatedStoragefileStream isoStream = new IsolatedStoragefileStream(path,System.Text.EnCoding.UTF8)) { xdoc.Save(sw); } } } } } /// <summary> /// 记录节点信息的类 重写了Equals方法 /// </summary> public class MemoryNode { public string name { set; get; } public string value { set; get; } public MemoryNode(string name,string value) { this.name = name; this.value = value; } public overrIDe bool Equals(Object node) { if (node == null) { return false; } MemoryNode newNode = (MemoryNode)node; if(this.name.Equals(newNode.name)) { return true; } return false; } }}总结
以上是内存溢出为你收集整理的silverlight IsolatedStorageFile 创建及 *** 作xml全部内容,希望文章能够帮你解决silverlight IsolatedStorageFile 创建及 *** 作xml所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)