silverlight IsolatedStorageFile 创建及操作xml

//记得添加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;
            
        }
       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值