//记得添加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 name, string value)
{
createFile(path);
if (exitsNode(path,name,value))
{
updateNode(path, name, value);//已经存在的节点更新即可
return;
}
using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
XDocument xdoc=null;
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(path, FileMode.Open, 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 name, string value)
{
createFile(path);
using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
XDocument xdoc = null;
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(path, FileMode.Open, isoFile))
{
XElement newElement = new XElement(name, 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, FileMode.Truncate, FileAccess.Write, isoFile))
{
using (StreamWriter sw = new StreamWriter(isoStream, 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
最新推荐文章于 2024-07-25 19:54:22 发布