using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Configuration; using System.Windows.Forms; namespace Dome { /// <summary> /// XML操作类 /// 两个构造函数中,一个是XML文件路径,一个文件路径加上节点路径,在重载的函数中有的需要设置节点路径 /// 方法为:xmlManager.NodePath = "\\子节点\子节点……" /// </summary> public sealed class XmlManager {
private readonly XmlDocument document = new XmlDocument();
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Windows.Forms;
namespace Dome
{
/// <summary>
/// XML操作类
/// 两个构造函数中,一个是XML文件路径,一个文件路径加上节点路径,在重载的函数中有的需要设置节点路径
/// 方法为:xmlManager.NodePath = "\\子节点\子节点……"
/// </summary>
public sealed class XmlManager
{
private readonly XmlDocument document = new XmlDocument();
private string nodePath;//节点路径
/// <summary>
/// 操作的XML文档路径=文件路径+文件全名
/// </summary>
public string XmlPath
{
get { return xmlPath;}
}
/// <summary>
/// XML文档中的节点路径"//根节点/子节点/子节点……"
/// </summary>
public string NodePath
{
get { return nodePath; }
set { nodePath = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="xmlPath">XML文档路径+xml文件全名</param>
public XmlManager(string xmlPath,string nodePath)
{
this.xmlPath = xmlPath;
this.nodePath = nodePath;
this.document.Load(xmlPath);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="xmlName">默认程序运行路径和XML文件名</param>
public XmlManager(string xmlPath)
{
this.xmlPath = xmlPath;
this.document.Load(xmlPath);
} /// <summary>
/// 构造方法
/// </summary>
public XmlManager()
{
this.document.Load(xmlPath);
}
/// <summary>
/// 读出指定路径XML文档的全部内容
/// </summary>
/// <returns>XML文档的全部内容</returns>
public string Out()
{
return this.document.OuterXml;
}
/// <summary>
/// 获取XML中节点的内容
/// </summary>
/// <param name="i">第i段数据</param>
/// <param name="j">第j个属性</param>
/// <param name="path">用户指定节点路径</param>
/// <returns></returns>
public string GetNode(int i,int j,string path)
{
try
{
XmlNodeList nodeList = this.document.SelectNodes(path);
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 获取XML中节点的内容
/// </summary>
/// <param name="i">第i段数据</param>
/// <param name="j">第j个属性</param>
/// <returns></returns>
public string GetNode(int i, int j)
{
try
{
XmlNodeList nodeList = this.document.SelectNodes(nodePath); return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 获取XML中节点的内容
/// </summary>
/// <param name="i">第i段数据</param>
/// <param name="nodeName">节点名</param>
/// <param name="nodePath">用户指定路径</param>
/// <returns></returns>
public string GetNode(int i,string nodePath, string nodeName)
{
try
{
XmlNodeList nodeList = this.document.SelectNodes(nodePath);
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText; }
return "nofind";
}
catch(Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 获取XML中节点的内容
/// </summary>
/// <param name="i">第i段数据</param>
/// <param name="nodeName">节点名</param>
/// <returns></returns>
public string GetNode(int i,string nodeName)
{
try
{
XmlNodeList nodeList = this.document.SelectNodes(nodePath);
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText; }
return "nofind";
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Windows.Forms;
namespace Dome
{
public sealed class XmlManager
{
/// <summary>
/// 修改指定节点的值
/// </summary>
/// <param name="nodeName">节点名称</param>
/// <param name="newValue">新值</param>
/// <returns>返回更改的节点数</returns>
public int SetNode(string nodeName, string newValue)
{
try
{
XmlNodeList nodeList = this.document.GetElementsByTagName(nodeName);
foreach (XmlNode node in nodeList)
{
node.InnerText = newValue;
}
this.document.Save(xmlPath);
return nodeList.Count;
}
catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 为指定的节点(集插)入一个子节点
/// </summary>
/// <param name="parentName">父节点</param>
/// <param name="nodeName">子节点名</param>
/// <param name="nodeVale">子节点值</param>
public void InsetNode(string parentName, string nodeName, string nodeVale)
{
XmlNodeList nodeList = this.document.GetElementsByTagName(parentName);
for (int i = 0; i < nodeList.Count; i++)
{
XmlElement newElement = document.CreateElement(nodeName);
//newElement.SetAttribute(nodeName,nodeVale);//会插入如<id id = "0012"/>的节点
nodeList.Item(i).AppendChild(newElement);//<id>002</id>
newElement.InnerText = nodeVale;
}
this.document.Save(this.xmlPath);
}
/// <summary>
/// 为指定的节点(集)插入一个子节点
/// </summary>
/// <param name="parentName">父节点</param>
/// <param name="nodeName">子节点名</param>
/// <param name="nodeVale">子节点值</param>
public void InsetNode(int i, string parentName, string nodeName, string nodeValue)
{
try
{
XmlNodeList nodeList = this.document.GetElementsByTagName(parentName);
XmlElement newElement = document.CreateElement(nodeName);
//newElement.SetAttribute(nodeName, nodeValue);
if (nodeList.Count > 0 && nodeList.Count <= i)
{
nodeList.Item(i - 1).AppendChild(newElement);
newElement.InnerText = nodeValue; this.document.Save(this.xmlPath);
}
else
{
Log.Info("无此节点");
}
}
catch (Exception e)
{
Log.Error(e);
} }
/// <summary>
/// 插入根节点(第二级根节点)
/// </summary>
/// <param name="rootName">节点名</param>
/// <param name="nodeName">子节点名</param>
/// <param name="nodeValue">子节点值</param>
public void InsertRootNode(string rootName, string[] nodeName, string[] nodeValue)
{
XmlElement root = document.DocumentElement;
XmlElement newRoot = document.CreateElement(rootName);
root.AppendChild(newRoot);
for (int i = 0; i < nodeName.Length; i++)
{
XmlElement newChild = document.CreateElement(nodeName[i]);
newRoot.AppendChild(newChild);
newChild.InnerText = nodeValue[i];
}
this.document.Save(this.xmlPath);
} public void DeleteNote(string parentName, string noteName)
{
XmlNodeList nodeList = document.GetElementsByTagName(parentName); foreach (XmlNode node in nodeList)
{
foreach (XmlNode nodechild in node.ChildNodes)
if (nodechild.Name == noteName)
{
node.RemoveChild(nodechild);
}
}
this.document.Save(this.xmlPath);
}
public void DeleteNote(string parmentName)
{
XmlNodeList nodeList = this.document.GetElementsByTagName(parmentName);
foreach (XmlNode node in nodeList)
node.RemoveAll();
this.document.Save(xmlPath);
}
public void DeleteAll()
{
XmlElement element = this.document.DocumentElement;
element.RemoveAll();
this.document.Save(this.xmlPath);
}
public Boolean Save(string xmlName, string rootElement)
{
try
{
string savePath = System.IO.Directory.GetCurrentDirectory() + "\\" + xmlName;
XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement(rootElement);
document.AppendChild(element);
document.Save(savePath);
return true;
}
catch (XmlException xe)
{
Log.Error(xe);
return false;
}
}
/// <summary>
/// 创建XML文档
/// </summary>
public static void CreateXmlDocument()
{
string time = DateTime.Now.ToString("yyyy-MM-dd");
string documentName = string.Format(@"{0}\{1}.xml", AppDomain.CurrentDomain.BaseDirectory, time);
if (!File.Exists(documentName))
{
StringBuilder xml = new StringBuilder();
xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><Videos></Videos>");
string xmltext = xml.ToString();
FileStream fs = new FileStream(documentName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(xmltext);
sw.Close();
fs.Close();
}
}
}