using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
namespace BLL
{
public class XmlDoc
{
///
/// 创建Xml文件
///
/// 创建文件路径
///
public void CreateXmlNode(string xmlPath, string element)
{
//实例化Xml文档类
XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath, Encoding.UTF8);
//创建Xml声明
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement(element);
xmlTextWriter.Flush();
xmlTextWriter.Close();
}
///
/// 新增Xml节点
///
/// 文件路径
///
///
public void AddXmlElement(string xmlPath, string singleNode, XmlNode node)
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(xmlPath);
xmlDocument.SelectSingleNode(singleNode).AppendChild(node);
xmlDocument.Save(xmlPath);
}
///
/// 新增Xml节点
///
///
///
///
public void AddXmlElement(string xmlPath, string singleNode, string element, string attribute, string value)
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(xmlPath);
XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
XmlElement xmlElement = xmlDocument.CreateElement(element); ;
xmlElement.SetAttribute(attribute, value);
xmlDocument.Save(xmlPath);
}
///
/// 读取XML文件的XmlDocument
///
/// Xml文件路径
///
public XmlDocument GetXmlDocument(string xmlPath)
{
try
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(GetMapPath(xmlPath));
return xmlDocument;
}
catch (System.Xml.XmlException exp)
{
throw exp;
}
catch (System.IO.IOException exp)
{
throw exp;
}
catch (ArgumentNullException exp)
{
throw exp;
}
catch (ArgumentException exp)
{
throw exp;
}
catch (Exception exp)
{
throw exp;
}
}
///
/// 读取XML文件的XmlNode
///
/// Xml文件路径
/// 选择匹配XPath表达式的第一个XmlNode
///
public XmlNode GetXmlNode(string xmlPath, string singleNode)
{
try
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(GetMapPath(xmlPath));
//获取文件内容
return xmlDocument.SelectSingleNode(singleNode);
}
catch (System.Xml.XPath.XPathException exp)
{
throw exp;
}
catch (System.Xml.XmlException exp)
{
throw exp;
}
catch (System.IO.IOException exp)
{
throw exp;
}
catch (ArgumentNullException exp)
{
throw exp;
}
catch (ArgumentException exp)
{
throw exp;
}
catch (Exception exp)
{
throw exp;
}
}
///
/// 读取XML文件的XmlElement
///
/// Xml文件路径
/// 选择匹配XPath表达式的第一个XmlNode
/// 节点名称
///
public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString)
{
try
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(GetMapPath(xmlPath));
//获取文件内容
XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
//提取节点名称下的属性值
return xmlNode[xmlNodeString];
}
catch (System.Xml.XPath.XPathException exp)
{
throw exp;
}
catch (System.Xml.XmlException exp)
{
throw exp;
}
catch (System.IO.IOException exp)
{
throw exp;
}
catch (ArgumentNullException exp)
{
throw exp;
}
catch (ArgumentException exp)
{
throw exp;
}
catch (Exception exp)
{
throw exp;
}
}
///
/// 读取XML文件的节点属性
///
/// Xml文件路径
/// 选择匹配XPath表达式的第一个XmlNode
/// 节点名称
/// 节点属性
///
public string GetAttribute(string xmlPath, string singleNode, string xmlNodeString, string variables)
{
try
{
//实例化Xml文档类
XmlDocument xmlDocument = new XmlDocument();
//加载Xml文件
xmlDocument.Load(GetMapPath(xmlPath));
//获取文件内容
XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
//提取节点名称下的属性值
return xmlNode[xmlNodeString].GetAttribute(variables);
}
catch (System.Xml.XPath.XPathException exp)
{
throw exp;
}
catch (System.Xml.XmlException exp)
{
throw exp;
}
catch (System.IO.IOException exp)
{
throw exp;
}
catch (ArgumentNullException exp)
{
throw exp;
}
catch (ArgumentException exp)
{
throw exp;
}
catch (Exception exp)
{
throw exp;
}
}
///
/// 设置XML节点
///
/// 节点名称
/// 节点值
/// 节点说明
public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string attribute, string value, string description)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(GetMapPath(xmlPath));
//获取文件内容
XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
//设置指定XML节点Value值
xmlNode[xmlNodeString].SetAttribute(attribute, value);
xmlNode[xmlNodeString].SetAttribute("Description", description);
//将设置后的XML节点保存
xmlDocument.Save(GetMapPath(xmlPath));
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
///
/// 设置XML节点
///
/// XML文件路径
/// 选择匹配XPath表达式的第一个XmlNode
/// 指定节点
/// 属性数组
/// 值数组
public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string[] attribute, string[] value)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(GetMapPath(xmlPath));
//获取文件内容
XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
for (int i = 0; i < attribute.Length; i++)
{
//设置指定XML节点Value值
xmlNode[xmlNodeString].SetAttribute(attribute[i], value[i]);
}
//将设置后的XML节点保存
xmlDocument.Save(xmlPath);
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
///
/// 通过XML名称获取XML物理路径,可无限添加,前提是XML都位于同一网站目录下
///
///
///
private string GetMapPath(string xmlName)
{
switch (xmlName)
{
case "GlobalVariables.xml":
xmlName = HttpContext.Current.Server.MapPath("~/XML/") + xmlName;
break;
default:
break;
}
return xmlName;
}
}
}
好了不多说了,如果有bug出现希望大家多多指点....