最近想学习一下如何操作xml文件,从网上找了些事例,然后整理了一下,弄了个不是很完整的类,希望跟大家能够一起分享一下,有什么不好的地方,还请大侠提出宝贵意见
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace shyc.cdw.xml
{
public class XMLFile
{
private XmlDocument XmlDoc;
private String _XmlFileName;
public String XmlFileName
{
get { return _XmlFileName; }
set { _XmlFileName = value; }
}
private String _RootName;
public String RootName
{
get { return _RootName; }
set { _RootName = value; }
}
private string _RootText;
public string RootText
{
get { return _RootText; }
set { _RootText = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="filename">xml文件的全名(含文件的路径)</param>
public XMLFile(string filename)
{
XmlDoc = new XmlDocument();
this._XmlFileName = filename;
}
/// <summary>
/// 创建xml文件
/// </summary>
public void CreateXMLFile()
{
XmlNode xmlnode;
XmlElement xmlelem;
XmlText xmltext;
XmlDoc = new XmlDocument();
//加入XML的声明段落
xmlnode = XmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlDoc.AppendChild(xmlnode);
xmlelem = XmlDoc.CreateElement("", _RootName, "");
xmltext = XmlDoc.CreateTextNode(_RootText);
xmlelem.AppendChild(xmltext);
XmlDoc.AppendChild(xmlelem);
//保存创建好的XML文档
XmlDoc.Save(_XmlFileName);
}
/// <summary>
/// 添加子节点
/// </summary>
/// <param name="pNodeName">父节点的节点名</param>
/// <param name="ItemName">新节点的名字</param>
/// <param name="Content">新节点属性值的集合</param>
/// <param name="NodeValue">新节点的值</param>
public void AddChildNode(string pNodeName,string ItemName,string[]Content,string NodeValue)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(pNodeName);
//当父节点存在的时候进行添加
if (node != null)
{
XmlElement elem = XmlDoc.CreateElement(ItemName);
foreach (string ss in Content)
{
if (ss != null)
{
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
}
}
if (NodeValue != null)
{
if (NodeValue != "") elem.InnerText = NodeValue;
}
node.AppendChild(elem);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,null );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
private object Exception()
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 增加根子节点
/// </summary>
/// <param name="ItemName">新节点名称</param>
/// <param name="Content">新节点属性集合</param>
/// <param name="NodeValue">新节点的值</param>
public void AddRootChildNode(string ItemName,string[]Content,string NodeValue)
{
XmlNode node = XmlDoc.SelectSingleNode(_RootName );
XmlElement elem = XmlDoc.CreateElement(ItemName);
foreach (string ss in Content)
{
if (ss != null)
{
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
}
}
if (NodeValue != null)
{
if (NodeValue != "") elem.InnerText = NodeValue;
}
node.AppendChild(elem);
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 更新某节点
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="NewNode">新节点</param>
public void UpdataNode(string xPath,XmlNode NewNode)
{
XmlDoc.Load(this._XmlFileName );
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.ReplaceChild(NewNode,node);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 删除某节点以及其子节点
/// </summary>
/// <param name="xPath"></param>
public void RemoveNode(string xPath)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.RemoveChild(node);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 获取指定节点的属性值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <returns></returns>
public string getNodeAttribute(string xPath,string name)
{
string result = null;
XmlDoc.Load(this._XmlFileName);
XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(n != null)
{
if(n.LocalName != "#comment" && n.Attributes[name] != null)
{
if(n.Attributes["active"] != null )//&& n.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
{
result = n.Attributes[name].Value;
}
}
}
return result;
}
/// <summary>
/// 写指定节点的属性值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <param name="values">属性新值</param>
public void setNodeAttribute(string xPath,string name,string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
if (node.LocalName != "#comment" && node.Attributes[name] != null)
{
if (node.Attributes["active"] != null)// && node.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
{
node.Attributes[name].Value = values;
}
}
else
{
}
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 添加指定节点的属性
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <param name="values">属性值</param>
public void AddNodeAttribute(string xPath, string name, string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if (node != null)
{
//添加新属性
if ((!name.Equals("")) && node.Attributes[name] == null)
{
XmlAttribute myAttribute = XmlDoc.CreateAttribute(name);
myAttribute.Value = values;
node.Attributes.Append(myAttribute);
}
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 判断是否有子节点
/// </summary>
/// <param name="xPath">节点名</param>
/// <returns>子节点的个数</returns>
public int ChildnodesNumber(string xPath)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
return node.ChildNodes.Count;
}
/// <summary>
/// 检测新增节点是否存在
/// </summary>
/// <param name="pNodeName">父节点名</param>
/// <param name="ItemName">新节点名</param>
/// <returns> 110新增子节点存在 100成功 99父节点不存在 </returns>
public int NodeExist(string pNodeName,string ItemName)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(pNodeName);
if (node != null)
{
int i;
for ( i = 0; i < node.ChildNodes.Count - 1; i++)
{
if (node.ChildNodes.Item(i).Name == ItemName)
{
return 110;
}
}
return 100;
}
else
{
return 99;
}
}
/// <summary>
/// 获取指定节点的值
/// </summary>
/// <param name="xPath">节点名</param>
/// <returns>节点的值</returns>
public string getNodeValue(string xPath)
{
string result = null;
XmlDoc.Load(this._XmlFileName);
XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(n != null)
{
result = n.InnerText;
}
return result;
}
/// <summary>
/// 写指定节点的值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="values">节点的新值</param>
public void setNodeValue(string xPath,string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if (node != null)
{
node.InnerText = values;
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
}
}
}
本人的QQ:280685904,期待与您的交流
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace shyc.cdw.xml
{
public class XMLFile
{
private XmlDocument XmlDoc;
private String _XmlFileName;
public String XmlFileName
{
get { return _XmlFileName; }
set { _XmlFileName = value; }
}
private String _RootName;
public String RootName
{
get { return _RootName; }
set { _RootName = value; }
}
private string _RootText;
public string RootText
{
get { return _RootText; }
set { _RootText = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="filename">xml文件的全名(含文件的路径)</param>
public XMLFile(string filename)
{
XmlDoc = new XmlDocument();
this._XmlFileName = filename;
}
/// <summary>
/// 创建xml文件
/// </summary>
public void CreateXMLFile()
{
XmlNode xmlnode;
XmlElement xmlelem;
XmlText xmltext;
XmlDoc = new XmlDocument();
//加入XML的声明段落
xmlnode = XmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlDoc.AppendChild(xmlnode);
xmlelem = XmlDoc.CreateElement("", _RootName, "");
xmltext = XmlDoc.CreateTextNode(_RootText);
xmlelem.AppendChild(xmltext);
XmlDoc.AppendChild(xmlelem);
//保存创建好的XML文档
XmlDoc.Save(_XmlFileName);
}
/// <summary>
/// 添加子节点
/// </summary>
/// <param name="pNodeName">父节点的节点名</param>
/// <param name="ItemName">新节点的名字</param>
/// <param name="Content">新节点属性值的集合</param>
/// <param name="NodeValue">新节点的值</param>
public void AddChildNode(string pNodeName,string ItemName,string[]Content,string NodeValue)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(pNodeName);
//当父节点存在的时候进行添加
if (node != null)
{
XmlElement elem = XmlDoc.CreateElement(ItemName);
foreach (string ss in Content)
{
if (ss != null)
{
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
}
}
if (NodeValue != null)
{
if (NodeValue != "") elem.InnerText = NodeValue;
}
node.AppendChild(elem);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,null );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
private object Exception()
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 增加根子节点
/// </summary>
/// <param name="ItemName">新节点名称</param>
/// <param name="Content">新节点属性集合</param>
/// <param name="NodeValue">新节点的值</param>
public void AddRootChildNode(string ItemName,string[]Content,string NodeValue)
{
XmlNode node = XmlDoc.SelectSingleNode(_RootName );
XmlElement elem = XmlDoc.CreateElement(ItemName);
foreach (string ss in Content)
{
if (ss != null)
{
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
}
}
if (NodeValue != null)
{
if (NodeValue != "") elem.InnerText = NodeValue;
}
node.AppendChild(elem);
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 更新某节点
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="NewNode">新节点</param>
public void UpdataNode(string xPath,XmlNode NewNode)
{
XmlDoc.Load(this._XmlFileName );
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.ReplaceChild(NewNode,node);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 删除某节点以及其子节点
/// </summary>
/// <param name="xPath"></param>
public void RemoveNode(string xPath)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.RemoveChild(node);
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 获取指定节点的属性值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <returns></returns>
public string getNodeAttribute(string xPath,string name)
{
string result = null;
XmlDoc.Load(this._XmlFileName);
XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(n != null)
{
if(n.LocalName != "#comment" && n.Attributes[name] != null)
{
if(n.Attributes["active"] != null )//&& n.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
{
result = n.Attributes[name].Value;
}
}
}
return result;
}
/// <summary>
/// 写指定节点的属性值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <param name="values">属性新值</param>
public void setNodeAttribute(string xPath,string name,string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
if (node.LocalName != "#comment" && node.Attributes[name] != null)
{
if (node.Attributes["active"] != null)// && node.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
{
node.Attributes[name].Value = values;
}
}
else
{
}
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 添加指定节点的属性
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="name">属性名</param>
/// <param name="values">属性值</param>
public void AddNodeAttribute(string xPath, string name, string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if (node != null)
{
//添加新属性
if ((!name.Equals("")) && node.Attributes[name] == null)
{
XmlAttribute myAttribute = XmlDoc.CreateAttribute(name);
myAttribute.Value = values;
node.Attributes.Append(myAttribute);
}
}
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
/// <summary>
/// 判断是否有子节点
/// </summary>
/// <param name="xPath">节点名</param>
/// <returns>子节点的个数</returns>
public int ChildnodesNumber(string xPath)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
return node.ChildNodes.Count;
}
/// <summary>
/// 检测新增节点是否存在
/// </summary>
/// <param name="pNodeName">父节点名</param>
/// <param name="ItemName">新节点名</param>
/// <returns> 110新增子节点存在 100成功 99父节点不存在 </returns>
public int NodeExist(string pNodeName,string ItemName)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(pNodeName);
if (node != null)
{
int i;
for ( i = 0; i < node.ChildNodes.Count - 1; i++)
{
if (node.ChildNodes.Item(i).Name == ItemName)
{
return 110;
}
}
return 100;
}
else
{
return 99;
}
}
/// <summary>
/// 获取指定节点的值
/// </summary>
/// <param name="xPath">节点名</param>
/// <returns>节点的值</returns>
public string getNodeValue(string xPath)
{
string result = null;
XmlDoc.Load(this._XmlFileName);
XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(n != null)
{
result = n.InnerText;
}
return result;
}
/// <summary>
/// 写指定节点的值
/// </summary>
/// <param name="xPath">节点名</param>
/// <param name="values">节点的新值</param>
public void setNodeValue(string xPath,string values)
{
XmlDoc.Load(this._XmlFileName);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if (node != null)
{
node.InnerText = values;
XmlTextWriter writer = new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
}
}
}