C#:XML操作类

C#:XML操作类
写的一个XML操作类,包括读取/插入/修改/删除。
 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace PuTianCheng
{
 /// <summary>
 /// XmlHelper 的摘要说明
 /// </summary>
 public class XmlHelper
 {
 public XmlHelper()
 {
 }
 /// <summary>
 /// 读取数据
 /// </summary>
 /// <param name="path">路径</param>
 /// <param name="node">节点</param>
 /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
 /// <returns>string</returns>
 /**************************************************
 * 使用示列:
 * XmlHelper.Read(path, "/Node", "")
 * XmlHelper.Read(path, "/Node/Element[@Attribute=‘Name‘]", "Attribute")
 ************************************************/
 public static string Read(string path, string node, string attribute)
 {
 string value = "";
 try
 {
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNode xn = doc.SelectSingleNode(node);
 value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
 }
 catch { }
 return value;
 }
 /// <summary>
 /// 插入数据
 /// </summary>
 /// <param name="path">路径</param>
 /// <param name="node">节点</param>
 /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
 /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
 /// <param name="value"></param>
 /// <returns></returns>
 /**************************************************
 * 使用示列:
 * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
 * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
 * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
 ************************************************/
 public static void Insert(string path, string node, string element, string attribute, string value)
 {
 try
 {
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNode xn = doc.SelectSingleNode(node);
 if (element.Equals(""))
 {
 if (!attribute.Equals(""))
 {
 XmlElement xe = (XmlElement)xn;
 xe.SetAttribute(attribute, value);
 }
 }
 else
 {
 XmlElement xe = doc.CreateElement(element);
 if (attribute.Equals(""))
 xe.InnerText = value;
 else
 xe.SetAttribute(attribute, value);
 xn.AppendChild(xe);
 }
 doc.Save(path);
 }
 catch { }
 }
 /// <summary>
 /// 修改数据
 /// </summary>
 /// <param name="path">路径</param>
 /// <param name="node">节点</param>
 /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
 /// <param name="value"></param>
 /// <returns></returns>
 /**************************************************
 * 使用示列:
 * XmlHelper.Insert(path, "/Node", "", "Value")
 * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
 ************************************************/
 public static void Update(string path, string node, string attribute, string value)
 {
 try
 {
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNode xn = doc.SelectSingleNode(node);
 XmlElement xe = (XmlElement)xn;
 if (attribute.Equals(""))
 xe.InnerText = value;
 else
 xe.SetAttribute(attribute, value);
 doc.Save(path);
 }
 catch { }
 }
 /// <summary>
 /// 删除数据
 /// </summary>
 /// <param name="path">路径</param>
 /// <param name="node">节点</param>
 /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
 /// <param name="value"></param>
 /// <returns></returns>
 /**************************************************
 * 使用示列:
 * XmlHelper.Delete(path, "/Node", "")
 * XmlHelper.Delete(path, "/Node", "Attribute")
 ************************************************/
 public static void Delete(string path, string node, string attribute)
 {
 try
 {
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNode xn = doc.SelectSingleNode(node);
 XmlElement xe = (XmlElement)xn;
 if (attribute.Equals(""))
 xn.ParentNode.RemoveChild(xn);
 else
 xe.RemoveAttribute(attribute);
 doc.Save(path);
 }
 catch { }
 }
 }
}
==================================================
XmlFile.xml:
<?xml version="1.0" encoding="utf-8"?>
<Root />
==================================================
使用方法:
string xml = Server.MapPath("XmlFile.xml");
//插入元素
//XmlHelper.Insert(xml, "/Root", "Studio", "", "");
//插入元素/属性
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
//XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "五月软件");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=‘五月软件]", "Master", "", "五月");
//插入属性
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=‘小路工作室‘]", "", "Url", "http://www.wzlu.com/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=‘丁香鱼工作室‘]", "", "Url", "http://www.luckfish.net/");
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=‘五月软件]", "", "Url", "http://www.vs2005.com.cn/");
//修改元素值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name=‘五月软件]/Master", "", "Wuyue");
//修改属性值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name=‘五月软件]", "Url", "http://www.vs2005.com.cn/");
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name=‘五月软件]", "Name", "MaySoft");
//读取元素值
//Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
//读取属性值
//Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
//读取特定属性值
//Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name=‘丁香鱼工作室‘]", "Url") + "</div>");
//删除属性
//XmlHelper.Delete(xml, "/Root/Studio/Site[@Name=‘小路工作室‘]", "Url");
//删除元素
//XmlHelper.Delete(xml, "/Root/Studio", "");
View Code

C#中xml写入treeview,treeview写入xml

C#:XML操作类
using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Windows.Forms;   
using System.Xml;   
  
  
namespace treeview2   //你的项目名
{   
    class TreeExXMLCls   
    {   
        private TreeView thetreeview;   
        private string xmlfilepath;   
        XmlTextWriter textWriter;   
        XmlNode Xmlroot;   
        XmlDocument textdoc;   
  
        public TreeExXMLCls()   
        {   
            //----构造函数   
            textdoc = new XmlDocument();   
  
        }   
  
        ~TreeExXMLCls()   
        {   
            //----析构函数   
        }  
 
        #region 遍历treeview并实现向XML的转化   
        /// <summary>      
        /// 遍历treeview并实现向XML的转化   
        /// </summary>      
        /// <param name="TheTreeView">树控件对象</param>      
        /// <param name="XMLFilePath">XML输出路径</param>      
        /// <returns>0表示函数顺利执行</returns>      
  
        public int TreeToXML(TreeView TheTreeView, string XMLFilePath)   
        {   
            //-------初始化转换环境变量   
            thetreeview = TheTreeView;   
            xmlfilepath = XMLFilePath;   
            textWriter = new XmlTextWriter(xmlfilepath, null);   
  
            //-------创建XML写操作对象   
            textWriter.Formatting = Formatting.Indented;   
  
            //-------开始写过程,调用WriteStartDocument方法   
            textWriter.WriteStartDocument();   
  
            //-------写入说明   
            textWriter.WriteComment("this XML is created from a tree");   
            
            //-------添加第一个根节点   
            textWriter.WriteStartElement("TreeExXMLCls");   
            textWriter.WriteEndElement();   
  
            //------ 写文档结束,调用WriteEndDocument方法   
            textWriter.WriteEndDocument();   
  
            //-----关闭输入流   
            textWriter.Close();   
  
            //-------创建XMLDocument对象   
            textdoc.Load(xmlfilepath);   
  
            //------选中根节点   
            XmlElement Xmlnode=textdoc.CreateElement(thetreeview.Nodes[0].Text);   
            Xmlroot = textdoc.SelectSingleNode("TreeExXMLCls");   
  
            //------遍历原treeview控件,并生成相应的XML   
            TransTreeSav(thetreeview.Nodes, (XmlElement)Xmlroot);   
            return 0;   
        }   
  
        private int TransTreeSav(TreeNodeCollection nodes, XmlElement ParXmlnode)    
        {   
  
            //-------遍历树的各个故障节点,同时添加节点至XML   
            XmlElement xmlnode;   
            Xmlroot = textdoc.SelectSingleNode("TreeExXMLCls");   
  
            foreach (TreeNode node in nodes)   
            {   
                xmlnode = textdoc.CreateElement(node.Text);   
                ParXmlnode.AppendChild(xmlnode);   
  
                if (node.Nodes.Count > 0)   
                {   
                    TransTreeSav(node.Nodes, xmlnode);   
                }   
            }   
            textdoc.Save(xmlfilepath);   
            return 0;   
        }  
 
        #endregion  
 
        #region 遍历XML并实现向tree的转化   
        /// <summary>      
        /// 遍历treeview并实现向XML的转化   
        /// </summary>      
        /// <param name="XMLFilePath">XML输出路径</param>      
        /// <param name="TheTreeView">树控件对象</param>      
        /// <returns>0表示函数顺利执行</returns>      
  
        public int XMLToTree(string XMLFilePath,TreeView TheTreeView)   
        {   
            //-------重新初始化转换环境变量   
            thetreeview = TheTreeView;
            xmlfilepath = XMLFilePath;   
  
            //-------重新对XMLDocument对象赋值   
            textdoc.Load(xmlfilepath);

            XmlNode root = textdoc.SelectSingleNode("Authors_Table");//读的xml的节点名
              
            foreach (XmlNode subXmlnod in root.ChildNodes)   
            {   
                TreeNode trerotnod = new TreeNode();   
                trerotnod.Text = subXmlnod.Name;   
                thetreeview.Nodes.Add(trerotnod);   
                TransXML(subXmlnod.ChildNodes, trerotnod);   
    
            }   
            return 0;   
        }   
  
        private int TransXML(XmlNodeList Xmlnodes,TreeNode partrenod)   
        {   
            //------遍历XML中的所有节点,仿照treeview节点遍历函数   
            foreach (XmlNode xmlnod in Xmlnodes)   
            {   
                TreeNode subtrnod = new TreeNode();   
                subtrnod.Text = xmlnod.Name+xmlnod.value;   
                partrenod.Nodes.Add(subtrnod);   
  
                if (xmlnod.ChildNodes.Count > 0)   
                {   
                    TransXML(xmlnod.ChildNodes, subtrnod);   
                }   
            }   
            return 0;   
        }  
        #endregion   
    }   
} 

上面是新建了个类。写完后要调用类方法

 private void button1_Click(object sender, EventArgs e)
        {
            TreeExXMLCls a = new TreeExXMLCls();
            a.XMLToTree("D:\\实验室\\匡老师项目\\wxample2.xml",treeView1);
        }

按钮单击事件激活方法。就可以把节点显示到treeview里了。
View Code

 

C#:XML操作类

上一篇:Android 完全退出应用


下一篇:C#重绘TabControl