网站地图    收藏   

主页 > 前端 > css教程 >

Xml读取和写入以及新建 - html/css语言栏目:html

来源:自学PHP网    时间:2015-04-14 14:51 作者: 阅读:

[导读] 有时候要用到Xml的读取和写入,甚至有时候需要新建一个Xml,但是好长时间不用这东西,都忘了,所以就写了一个类库,想用的时候直接拿过来用就OK了下面是代码[html]using System;using S...

有时候要用到Xml的读取和写入,甚至有时候需要新建一个Xml,但是好长时间不用这东西,都忘了,所以就写了一个类库,想用的时候直接拿过来用就OK了

下面是代码


[html]
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml; 
 
namespace Xmlleiku 

    public class Xmllei 
    { 
        public static string xmlpath = string.Empty; 
        /// <summary> 
        /// 初始化xml的物理地址 
        /// </summary> 
        /// <param name="path">物理地址</param> 
        public Xmllei(string path) 
        { 
            xmlpath = path; 
        } 
        /// <summary> 
        /// 读取xml中的内容 
        /// </summary> 
        /// <param name="element">节点的名字</param> 
        public Dictionary<int, string[]> Reader(string element) 
        { 
            XmlDocument xodc = new XmlDocument(); 
            xodc.Load(xmlpath); 
            XmlNodeList lis = xodc.GetElementsByTagName(element); 
            Dictionary<int, string[]> dic = new Dictionary<int, string[]>(); 
            int j = 0; 
            foreach (XmlNode item in lis) 
            { 
                string[] a = new string[item.ChildNodes.Count]; 
                for (int i = 0; i < item.ChildNodes.Count; i++) 
                { 
                    a[i] = item.ChildNodes.Item(i).InnerText; 
                } 
                dic.Add(j++, a); 
            } 
            return dic; 
        } 
        /// <summary> 
        /// 写入已有的Xml文档 
        /// </summary> 
        /// <param name="element">对应的标签组</param> 
        /// <param name="meirong">与标签对应的内容</param> 
        /// <returns></returns> 
        public int Write(string[] element, List<string[]> meirong) 
        { 
            int j=0; 
            try 
            { 
                XmlDocument xodc = new XmlDocument(); 
                xodc.Load(xmlpath); 
                foreach (string[] item1 in meirong) 
                { 
                    int i = 1; 
                    XmlElement a = xodc.CreateElement(element[0]); 
                    foreach (string item2 in item1) 
                    { 
                        XmlElement b = xodc.CreateElement(element[i++]); 
                        b.AppendChild(xodc.CreateTextNode(item2)); 
                        a.AppendChild(b); 
                    } 
                    j++; 
                    xodc.DocumentElement.AppendChild(a); 
                    xodc.Save(xmlpath); 
                } 
                return j; 
            } 
            catch 
            { 
                return 0; 
            } 
        } 
        /// <summary> 
        /// 创建一个新的Xml文档 
        /// </summary> 
        /// <param name="element">包含Xml文档的根目录的标签组</param> 
        /// <param name="meirong">内容数组</param> 
        /// <returns></returns> 
        public int Create(string[] element,List<string[]> meirong) 
        { 
            int j = 0; 
            XmlWriterSettings sttingsw = new XmlWriterSettings(); 
            sttingsw.Encoding = System.Text.Encoding.UTF8; 
            sttingsw.Indent = true; 
            using (XmlWriter writer = XmlWriter.Create(xmlpath)) 
            { 
                writer.WriteStartDocument(); 
                writer.WriteStartElement(element[0]); 
                foreach (string[] item1 in meirong) 
                { 
                     
                    int i = 2; 
                    writer.WriteStartElement(element[1]); 
                    foreach (string item2 in item1) 
                    { 
                        writer.WriteStartElement(element[i++]); 
                        writer.WriteString(item2); 
                        writer.WriteEndElement(); 
                    } 
                    writer.WriteEndElement(); 
                    j++; 
                } 
                writer.WriteEndElement(); 
                writer.WriteEndDocument(); 
            } 
            return j; 
        } 
    } 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Xmlleiku
{
    public class Xmllei
    {
        public static string xmlpath = string.Empty;
        /// <summary>
        /// 初始化xml的物理地址
        /// </summary>
        /// <param name="path">物理地址</param>
        public Xmllei(string path)
        {
            xmlpath = path;
        }
        /// <summary>
        /// 读取xml中的内容
        /// </summary>
        /// <param name="element">节点的名字</param>
        public Dictionary<int, string[]> Reader(string element)
        {
            XmlDocument xodc = new XmlDocument();
            xodc.Load(xmlpath);
            XmlNodeList lis = xodc.GetElementsByTagName(element);
            Dictionary<int, string[]> dic = new Dictionary<int, string[]>();
            int j = 0;
            foreach (XmlNode item in lis)
            {
                string[] a = new string[item.ChildNodes.Count];
                for (int i = 0; i < item.ChildNodes.Count; i++)
                {
                    a[i] = item.ChildNodes.Item(i).InnerText;
                }
                dic.Add(j++, a);
            }
            return dic;
        }
        /// <summary>
        /// 写入已有的Xml文档
        /// </summary>
        /// <param name="element">对应的标签组</param>
        /// <param name="meirong">与标签对应的内容</param>
        /// <returns></returns>
        public int Write(string[] element, List<string[]> meirong)
        {
            int j=0;
            try
            {
                XmlDocument xodc = new XmlDocument();
                xodc.Load(xmlpath);
                foreach (string[] item1 in meirong)
                {
                    int i = 1;
                    XmlElement a = xodc.CreateElement(element[0]);
                    foreach (string item2 in item1)
                    {
                        XmlElement b = xodc.CreateElement(element[i++]);
                        b.AppendChild(xodc.CreateTextNode(item2));
                        a.AppendChild(b);
                    }
                    j++;
                    xodc.DocumentElement.AppendChild(a);
                    xodc.Save(xmlpath);
                }
                return j;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 创建一个新的Xml文档
        /// </summary>
        /// <param name="element">包含Xml文档的根目录的标签组</param>
        /// <param name="meirong">内容数组</param>
        /// <returns></returns>
        public int Create(string[] element,List<string[]> meirong)
        {
            int j = 0;
            XmlWriterSettings sttingsw = new XmlWriterSettings();
            sttingsw.Encoding = System.Text.Encoding.UTF8;
            sttingsw.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(xmlpath))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(element[0]);
                foreach (string[] item1 in meirong)
                {
                   
                    int i = 2;
                    writer.WriteStartElement(element[1]);
                    foreach (string item2 in item1)
                    {
                        writer.WriteStartElement(element[i++]);
                        writer.WriteString(item2);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    j++;
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
            return j;
        }
    }
}


 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论