using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Common
{
/// <summary>
/// 节点属性类
/// </summary>
public class Attributes
{
string attributename;
/// <summary>
/// 节点属性名称
/// </summary>
public string Attributename
{
get { return attributename; }
set { attributename = value; }
}
string attributevalue;
/// <summary>
/// 节点属性值
/// </summary>
public string Attributevalue
{
get { return attributevalue; }
set { attributevalue = value; }
}
public Attributes(string attributename, string attributevalue)
{
this.attributename = attributename;
this.attributevalue = attributevalue;
}
public Attributes()
{ }
}
public class UserXml
{
/// <summary>
/// 创建XML文件
/// </summary>
/// <param name="filename">XML文件路径</param>
/// <param name="encoding">编码方式</param>
/// <param name="RootNode">根节点名称</param>
/// <returns>XMLDOCUMENT</returns>
public XmlDocument CreateXmlFile(string filename,string encoding,string RootNode)
{
XmlDocument xmldoc = new XmlDocument();
XmlNode node = xmldoc.CreateXmlDeclaration("1.0", encoding, null);
xmldoc.AppendChild(node);
XmlNode root = xmldoc.CreateElement(RootNode);
xmldoc.AppendChild(root);
xmldoc.Save(filename);
return xmldoc;
}
/// <summary>
/// 加载XML文件
/// </summary>
/// <param name="filename">Xml文件路径</param>
/// <returns></returns>
public XmlDocument LoadXml(string filename)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);
return xmldoc;
}
/// <summary>
/// 添加新节点
/// </summary>
/// <param name="xmldoc">XMLDOCUMENT对象</param>
/// <param name="parentnode">新节点的父节点</param>
/// <param name="name">新节点的名称</param>
/// <param name="value">新节点的值</param>
/// <param name="hasAttributes">是否有属性</param>
/// <param name="attributes">节点属性类的数组</param>
/// <returns>新节点</returns>
public XmlNode CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value, bool hasAttribute,
params Attributes[] attributes)
{
XmlNode newnode = xmldoc.CreateElement(name);
newnode.InnerText = value;
if (hasAttribute)
{
foreach (Attributes att in attributes)
{
XmlAttribute tempattribute = xmldoc.CreateAttribute(att.Attributename);
tempattribute.InnerText = att.Attributevalue;
newnode.Attributes.Append(tempattribute);
}
}
parentnode.AppendChild(newnode);
return newnode;
}
/// <summary>
/// 查询单个节点
/// </summary>
/// <param name="xmldoc">XmlDocument文档对象</param>
/// <param name="xpath">Xpath表达式</param>
/// <returns></returns>
public XmlNode SelectNode(XmlDocument xmldoc, string xpath)
{
XmlNode node = xmldoc.SelectSingleNode(xpath);
return node;
}
public XmlNodeList SelectNodeList(XmlDocument xmldoc, string xpath)
{
XmlNodeList list = xmldoc.SelectNodes(xpath);
return list;
}
}
}