using System;
using System.Xml;
using System.Data;
using System.IO;
namespace _XINYANGT_XML_Manager
{
/// <summary>
/// XmlFile 的摘要说明。
/// </summary>
public class XmlFile
{
private string _filename = null;
private XmlDocument XmlDoc;
public XmlFile(string filename)
{
XmlDoc = new XmlDocument();
this._filename = filename;
}
//增加子节点
public void AddChildNode(string xPath,string ItemName,string[]Content,string NodeValue)
{
try
{
XmlDoc.Load(this._filename);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
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._filename,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
catch(Exception e)
{
// 异常写如日志
}
}
//更新某节点
public void UpdataNode(string xPath,XmlNode NewNode)
{
XmlDoc.Load(this._filename);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.ReplaceChild(NewNode,node);
}
XmlTextWriter writer = new XmlTextWriter(this._filename,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
//删除某节点以及其子节点
public void RemoveNode(string xPath)
{
XmlDoc.Load(this._filename);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.ParentNode.RemoveChild(node);
}
XmlTextWriter writer = new XmlTextWriter(this._filename,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
//获取指定节点的属性值
public string getNodeAttribute(string xPath,string name)
{
string result = null;
try
{
XmlDoc.Load(this._filename);
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;
}
else
{
// 异常写如日志
}
}
}
}
catch(Exception e)
{
// 异常写如日志
}
return result;
}
//写指定节点的属性值
public void setNodeAttribute(string xPath,string name,string values)
{
try
{
XmlDoc.Load(this._filename);
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
{
// 异常写如日志
}
}
//添加新属性
if((!name.Equals("")) && node.Attributes[name] == null)
{
/*
XmlAttribute NewAttrib;
NewAttrib =Name = name.ToString().Trim();
NewAttrib.Value = values;
node.Attributes.Append(NewAttrib);
*/
}
}
XmlTextWriter writer = new XmlTextWriter(this._filename,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
catch(Exception e)
{
// 异常写如日志
}
}
public string ChildEx(string xPath)//判断是否有子节点
{
try
{
XmlDoc.Load(this._filename);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node.ChildNodes.Count > 0)
{
return node.ChildNodes.Count.ToString();
}
else
{
return node.ChildNodes.Count.ToString();
}
}
catch
{
return "";
}
}
//获取指定节点的值
public string getNodeValue(string xPath)
{
string result = null;
try
{
XmlDoc.Load(this._filename);
XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(n != null)
{
result = n.InnerText;
}
}
catch(Exception e)
{
// 异常写如日志
}
return result;
}
//写指定节点的值
public void setNodeValue(string xPath,string values)
{
try
{
XmlDoc.Load(this._filename);
XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
if(node != null)
{
node.InnerText = values;
XmlTextWriter writer = new XmlTextWriter(this._filename,null);
writer.Formatting = Formatting.Indented;
XmlDoc.Save(writer);
writer.Close();
writer = null;
}
}
catch(Exception e)
{
// 异常写如日志
}
}
}
}