最近发现操作XML比较多,并且操作方式都差不多,也就是读、编、删的问题,所以对XML操作再次封装,做了一个ControllerXML.cs.dll,这样一来下次引入DLL,就可以省略去很多麻烦的问题,整体来讲还比较有用,但XML文档结构比较固定,耦合度还是比较高,不利于扩展,不过我对XML的大部分操作也没有那么复杂,暂且就这样吧:
类名:
CXML.ControllerXML
成员方法:
public ArrayList GetAllXMLNodes(string path,string fatherNodes,string sonNodes, string attribute,string nodes)
public void AddXMLNode(string path, string fatherNodes, string sonNodes, string[] attribute, string[] attributeValue, string[] nodes, string[] nodesValue)
public void DelXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue)
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string newAttributeValue)
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string nodes, string newAttributeValue)
下面是类库文件:
ControllerXML.cs
--------------------------------------------------------------------------------
/*XML文件结构示例:
* Schedule为父节点
* KeyData为子节点
* year,month,day,hour,minute为属性
* offset,value为节点
<?xml version="1.0" encoding="utf-8" ?>
<Schedule>
<KeyData year="2010" month="9" day="1" hour="0" minute="0">
<offset>100</offset>
<value>这是一个测试数据</value>
</KeyData>
</Schedule>
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;//引入XML命名空间
using System.Collections;//引入数组空间
namespace CXML
{
public struct Nodes
{
public string attributeValue;
public string nodeValue;
}
public class ControllerXML
{
/// <summary>
/// 获取父节点下所有子节点的指定属性或节点值
/// </summary>
/// <param name="path">路径</param>
/// <param name="fatherNodes">父节点</param>
/// <param name="attribute">属性名/null</param>
/// <param name="Nodes">节点名/null</param>
/// <returns></returns>
public ArrayList GetAllXMLNodes(string path,string fatherNodes,string sonNodes, string attribute,string nodes)
{
ArrayList list = new ArrayList();
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);//读取XML文档
XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
if (xnf.Name == sonNodes)
{
Nodes node = new Nodes();
XmlElement SNode = (XmlElement)xnf;
node.attributeValue = SNode.GetAttribute(attribute);
XmlNodeList xnf1 = SNode.ChildNodes;
foreach (XmlNode xn1 in xnf1)
{
if (xn1.Name == nodes)
{
node.nodeValue = xn1.InnerText;
}
}
list.Add(node);
}
}
return list;
}
/// <summary>
/// 添加子节点和添加子节点的指定的属性和节点和添加子节点到父节点
/// 注意:属性名数组和属性值数组是对照关系,节点亦同
/// </summary>
/// <param name="path">路径</param>
/// <param name="fatherNodes">父节点</param>
/// <param name="sonNodes">子节点</param>
/// <param name="attribute">属性名数组</param>
/// <param name="attributeValue">属性值数组</param>
/// <param name="nodes">节点名数组</param>
/// <param name="nodesValue">节点值数组</param>
public void AddXMLNode(string path, string fatherNodes, string sonNodes, string[] attribute, string[] attributeValue, string[] nodes, string[] nodesValue)
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);//读取XML文档
XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
XmlElement sonData = XMLDoc.CreateElement(sonNodes);
for (int i = 0; i < attributeValue.Length; i++)
{
sonData.SetAttribute(attribute[i], attributeValue[i]);
}
for (int j = 0; j < nodes.Length; j++)
{
XmlElement nodeData = XMLDoc.CreateElement(nodes[j]);
nodeData.InnerText = nodesValue[j];
//将该节点追加到上级节点
sonData.AppendChild(nodeData);
}
xn.AppendChild(sonData);
XMLDoc.Save(path);
}
/// <summary>
/// 删除子节点指定属性的所有节点和该子节点本身
/// </summary>
/// <param name="path">路径</param>
/// <param name="fatherNodes">父节点</param>
/// <param name="sonNodes">子节点</param>
/// <param name="attribute">属性</param>
/// <param name="attributeValue">值</param>
public void DelXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue)
{
ArrayList arl = new ArrayList();
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);
XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
if (xn1.Name == sonNodes)
{
XmlElement sonData = (XmlElement)xn1;
if (sonData.GetAttribute(attribute) == attributeValue)
{
//sonData.RemoveAllAttributes ();
//sonData.RemoveAll();
arl.Add(xn1);
}
}
}
foreach (XmlNode xn1 in arl)
{
xn.RemoveChild(xn1);
}
XMLDoc.Save(path);
}
/// <summary>
/// 修改指定父节点的子节点的属性值
/// </summary>
/// <param name="path">路径</param>
/// <param name="fatherNodes">父节点</param>
/// <param name="sonNodes">子节点</param>
/// <param name="attribute">属性</param>
/// <param name="attributeValue">属性值</param>
/// <param name="newAttributeValue">新的属性值</param>
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string newAttributeValue)
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);//读取XML文档
XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
if (xnf.Name == sonNodes)
{
XmlElement sonData = (XmlElement)xnf;
if (sonData.GetAttribute(attribute) == attributeValue)
{
sonData.SetAttribute(attribute, newAttributeValue);
}
}
}
XMLDoc.Save(path);
}
/// <summary>
/// 修改指定属性的指定节点值(重载)
/// </summary>
/// <param name="path">路径</param>
/// <param name="fatherNodes">父节点</param>
/// <param name="sonNodes">子节点</param>
/// <param name="attribute">属性</param>
/// <param name="attributeValue">属性值</param>
/// <param name="nodes">节点</param>
/// <param name="newAttributeValue">新节点值</param>
public void EditXMLNode(string path, string fatherNodes, string sonNodes, string attribute, string attributeValue, string nodes, string newAttributeValue)
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);//读取XML文档
XmlNode xn = XMLDoc.SelectSingleNode(fatherNodes);
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
if (xnf.Name == sonNodes)
{
XmlElement sonData = (XmlElement)xnf;
if (sonData.GetAttribute(attribute) == attributeValue)
{
XmlNodeList nls = sonData.ChildNodes;
foreach (XmlNode xn1 in nls)
{
XmlElement node1 = (XmlElement)xn1;
if (node1.Name == nodes)
{
node1.InnerText = newAttributeValue;
break;
}
}
break;
}
}
}
XMLDoc.Save(path);
}
}
}
下面是一个Demo
XMLFile1.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Schedule>
<KeyData year="2010" month="9" day="1" hour="0" minute="0">
<offset>100</offset>
<value>这是一个测试数据</value>
</KeyData>
<KeyData year="2010" month="9" day="1" hour="0" minute="1">
<offset>100</offset>
<value>这是一个测试数据2</value>
</KeyData>
<KeyData year="2010" month="9" day="2" hour="0" minute="1">
<offset>1000</offset>
<value>这是一个测试数据3</value>
</KeyData>
<KeyData year="2010" month="7" day="1" hour="0" minute="0">
<offset>100</offset>
<value>这是一个测试数据</value>
</KeyData>
<KeyData year="2010" month="9" day="1" hour="0" minute="1">
<offset>100</offset>
<value>这是一个测试数据2</value>
</KeyData>
<KeyData year="2010" month="7" day="2" hour="0" minute="1">
<offset>1000</offset>
<value>这是一个测试数据3</value>
</KeyData>
</Schedule>
Program.cs
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using CXML;
using System.Collections;//引入数组空间
namespace Demo
{
class Program
{
static void Main(string[] args)
{
/*
//读取
string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
string fath ="Schedule";
Console.WriteLine ("输入子节点:");
string son =Console.ReadLine();
Console.WriteLine ("输入属性:");
string Attri= Console.ReadLine();
Console.WriteLine("输入节点:");
string nod = Console.ReadLine();
ControllerXML cXML = new ControllerXML();
ArrayList list = new ArrayList();
list = cXML.GetAllXMLNodes(path,fath,son, Attri, nod);
foreach (CXML.Nodes node in list)
{
Console.WriteLine("---节点---");
Console.WriteLine("属性值为:{0}", node.attributeValue);
Console.WriteLine("子节点值为:{0}", node.nodeValue );
}
Console.Read();
//添加
string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
string father = "Schedule";
string son = "KeyData";
string[] Attri={"year","month"};
string[] Attri_V = { "1990", "3" };
string[] N = { "offset", "value" };
string[] N_V = { "200", "30fgdg" };
ControllerXML cXML = new ControllerXML();
cXML.AddXMLNode(path, father, son, Attri, Attri_V,N,N_V );
Console.WriteLine("成功");
Console.Read();
//删除
string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
string father = "Schedule";
string son = "KeyData";
string Attri = "month";
string Attri_V = "7";
ControllerXML cXML = new ControllerXML();
cXML.DelXMLNode(path, father,son, Attri, Attri_V);
Console.WriteLine("成功");
Console.Read();
//修改节点
string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
ControllerXML cXML = new ControllerXML();
cXML.EditXMLNode(path, "Schedule", "KeyData", "month", "7", "8");
Console.WriteLine("成功");
Console.Read();
*/
//修改节点
string path = @"E:\【软件开发作品】\【C#项目】\ControllerXML.DLL\ControllerXML.cs\Demo\bin\Debug\XMLFile1.xml";
ControllerXML cXML = new ControllerXML();
cXML.EditXMLNode(path, "Schedule", "KeyData", "month", "8", "offset","修改成功");
Console.WriteLine("成功");
Console.Read();
}
}
}
下载该DLL文件的地址:http://cid-19bddddceef748b0.office.live.com/self.aspx/.Public/ControllerXML.cs.dll
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mane_yao/archive/2010/09/17/5891988.aspx