对xml文档的一些基本操作主要代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Windows .Forms ;

namespace 通讯录
{
    class TreeXML
    {
        TreeView theTreeView;
        XmlDocument xmlDocument;
        public TreeXML()
        {
            xmlDocument = new XmlDocument();
        }
        public void AddXmlNode(string XmlfilePth,string Nodename) {
            xmlDocument.Load(XmlfilePth );
            XmlNode root = xmlDocument.SelectSingleNode("List");
            XmlElement e1 = xmlDocument.CreateElement("组名");//创建一个xml中的元素

            e1.InnerText = Nodename;
            root.AppendChild(e1);//将指定的节点添加到该节点的子节点列的末尾
            xmlDocument.Save(XmlfilePth);
        }
        public void XmlToTree(string XmlfilePth ,TreeView treeView)
        {
            theTreeView = treeView;
            xmlDocument.Load(XmlfilePth );//加载xml文件件
            XmlNode root = xmlDocument.SelectSingleNode("List");//选择加载的xml文档中的List节点
            foreach (XmlNode  subnode in root.ChildNodes )
            {
                if (subnode.Name =="组名")
                {
                    TreeNode treenode = new TreeNode();// 定义一个treeview节点
                    treenode.Text = subnode.InnerText;//给treeview节点赋值
                    theTreeView.Nodes.Add(treenode );//添加节点到treeview控件当中

                }
               
            }
        }
        public void DeleXml(string Xmlfilepath, string Nodename)
        {

            xmlDocument.Load(Xmlfilepath);                                    //加载文件
            XmlNodeList xnl = xmlDocument.SelectSingleNode("List").ChildNodes;//获取节点名字为List的所有子节点
            foreach (XmlNode xd in xnl)                                        //遍历所有的节点找出名字为为Nodename的节点
            {
                XmlElement xe = (XmlElement)xd;                              //将节点类型转换为元素进行操作
                if (xe.InnerText == Nodename)
                {
                    xe.ParentNode.RemoveChild(xe);                            //删除该节点
                    xmlDocument.Save(Xmlfilepath);                            //保存文件

                }

            }
        }
        public void AlterXml(string Xmlfilepath, string OldNodename, string NewNodename)   //修改名字
        {
            xmlDocument.Load(Xmlfilepath);
            XmlNodeList xnl = xmlDocument.SelectSingleNode("List").ChildNodes;
            foreach (XmlNode xd in xnl)
            {
                XmlElement xe = (XmlElement)xd;
                if (xe.InnerText == OldNodename)
                {
                    xe.InnerText = NewNodename;                     //将节点的名字改为自己输入的名字
                    xmlDocument.Save(Xmlfilepath);
                }
            }

        }
        public DataTable Getpersoninfo(string Xmlfilepath, string Nodename)
        {
            xmlDocument.Load(Xmlfilepath);
            XmlNodeList xnl = xmlDocument.SelectSingleNode("CONTENTS").ChildNodes;
            DataTable dt = new DataTable();                                      //定义一个datatable 添加9列如下
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("出生日期", typeof(string));
            dt.Columns.Add("电话", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("QQ", typeof(string));
            dt.Columns.Add("性别", typeof(string));
            dt.Columns.Add("地址", typeof(string));
            dt.Columns.Add("备注", typeof(string));
            foreach (XmlNode xd in xnl)                            //遍历contents节点下的所有子节点
            {
                XmlElement xe = (XmlElement)xd;  
                if (xe.GetAttribute("分组") == Nodename)      //getAttribute() 方法通过名称获取属性的值  当分组的属性值是Nodename的值得时候将该节点下所有节点的信息添加到datatable当中
                {
                    DataRow myrow = dt.NewRow();
                    myrow["ID"] = xe.ChildNodes.Item(0).InnerText;
                    myrow["姓名"] = xe.ChildNodes.Item(1).InnerText;
                    myrow["出生日期"] = xe.ChildNodes.Item(2).InnerText;
                    myrow["电话"] = xe.ChildNodes.Item(3).InnerText;
                    myrow["Email"] = xe.ChildNodes.Item(4).InnerText;
                    myrow["QQ"] = xe.ChildNodes.Item(5).InnerText;
                    myrow["性别"] = xe.ChildNodes.Item(6).InnerText;
                    myrow["地址"] = xe.ChildNodes.Item(7).InnerText;
                    myrow["备注"] = xe.ChildNodes.Item(8).InnerText;
                    dt.Rows.Add(myrow);
                }
            }
            return dt;
        }
        public DataTable Getallpersoninfo(string Xmlfilepath)
        {
            xmlDocument.Load(Xmlfilepath);
            XmlNodeList xnl = xmlDocument.SelectSingleNode("CONTENTS").ChildNodes;
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("出生日期", typeof(string));
            dt.Columns.Add("电话", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("QQ", typeof(string));
            dt.Columns.Add("性别", typeof(string));
            dt.Columns.Add("地址", typeof(string));
            dt.Columns.Add("备注", typeof(string));
            foreach (XmlNode xd in xnl)
            {
                XmlElement xe = (XmlElement)xd;
                DataRow myrow = dt.NewRow();
                myrow["ID"] = xe.ChildNodes.Item(0).InnerText;
                myrow["姓名"] = xe.ChildNodes.Item(1).InnerText;
                myrow["出生日期"] = xe.ChildNodes.Item(2).InnerText;
                myrow["电话"] = xe.ChildNodes.Item(3).InnerText;
                myrow["Email"] = xe.ChildNodes.Item(4).InnerText;
                myrow["QQ"] = xe.ChildNodes.Item(5).InnerText;
                myrow["性别"] = xe.ChildNodes.Item(6).InnerText;
                myrow["地址"] = xe.ChildNodes.Item(7).InnerText;
                myrow["备注"] = xe.ChildNodes.Item(8).InnerText;
                dt.Rows.Add(myrow);

            }
            return dt;
        }//获取文件中所有的信息
    }
}

你可能感兴趣的:(xml)