XML操作类
发布时间:2009年09月24日点击数:
次xiaoxiaoniao

using ...System.Xml;
using System.Collections;


// === 保护成员 ====== 保护成员 ===#region === 保护成员 ===
protected string _strXmlFile;
// XML文件的路径
protected XmlDocument _objXmlDoc =
new XmlDocument();
// XML文件


// === 构造函数 ====== 构造函数 ===#region === 构造函数 ===

/**/
///
///
///
///
"xmlFilePath">文件路径(服务器的绝对路径)
public XmlControl(
string xmlFilePath)


...{
try

...{
_objXmlDoc.Load(xmlFilePath);
}
catch (System.Exception ex)

...{
throw ex;
}
_strXmlFile = xmlFilePath;
}

// === 查看是否存在 ====== 查看是否存在 ===#region === 查看是否存在 ===
public bool IsExist(
string xpath)

...{
XmlNode xn = this._objXmlDoc.SelectSingleNode(xpath);
return xn == null ? false : true;
}

// === 统计结点数目 ====== 统计结点数目 ===#region === 统计结点数目 ===

/**/
///
/// 获取结果个数
///
public int GetCount(
string xpath)

...{
XmlNodeList xnl = _objXmlDoc.SelectNodes(xpath);
return xnl == null ? 0 : xnl.Count;
}

//=== 查询数据 ====== 查询数据 ===#region === 查询数据 ===

//-- 根据Xpath查询 ---- 根据Xpath查询 --#region -- 根据Xpath查询 --

/**/
///
/// 查找对应结点的数据
///
///
"xPaht">结点选择
public DataView GetData(
string xPath)

...{
DataSet ds = GetDataSet(xPath);
// XmlReader read = new XmlNodeReader(_objXmlDoc.SelectSingleNode(xPath));
// ds.ReadXml(read);
return ds.Tables.Count == 0 ? null : ds.Tables[0].DefaultView; // 防止空结果
}

/**/
///
/// 获取某个结点下面的所有子元素对应的hashtable
///
///
"xpath">
///
public Hashtable GetElemet(
string xpath)

...{
Hashtable hs = new Hashtable();
XmlNode xn = _objXmlDoc.SelectSingleNode(xpath);
if (xn != null)

...{
foreach (XmlNode x in xn.ChildNodes)

...{
hs.Add(x.Name, x.InnerText);
}
}
return hs;
}

/**/
///
/// 获取某个元素的列表
///
///
"xpath">
///
public IList GetElemetList(
string xpath)

...{
IList list = new ArrayList();
XmlNodeList xnl = _objXmlDoc.SelectNodes(xpath);
if (xnl != null)

...{
foreach (XmlNode x in xnl)

...{
list.Add(x.InnerText);
}
}
return list;
}

/**/
///
/// 获取单个结点里面内容
///
///
"xpath">
/// 无则返回null
public string GetSingleNode(
string xpath)

...{
XmlNode xn = _objXmlDoc.SelectSingleNode(xpath);
if (xn != null)

...{
return xn.InnerText;
}
return null;
}

// -- 获取对应DataSet ---- 获取对应DataSet --#region -- 获取对应DataSet --
// 只能获取列相等的记录
private DataSet GetDataSet(
string xPath)

...{
XmlNodeList xnl = _objXmlDoc.SelectNodes(xPath);
// XmlTextReader reader = new XmlTextReader(
XmlDocument xd = new XmlDocument();
XmlNode rootNode = xd.CreateNode(XmlNodeType.Element, "root", null);
DataSet ds = new DataSet();
for (int i = 0; i < xnl.Count; i++)

...{
// read = new XmlNodeReader(xnl.Item(i));
rootNode.AppendChild(xd.ImportNode(xnl.Item(i), true));
// if(i == 0) // 初始化DS

// ...{
// ds.ReadXml(read);
// }
// else // 为DS的表添加行

// ...{
// DataSet dss = new DataSet();
// dss.ReadXml(read);
// ds.Tables[0].Rows.Add(dss.Tables[0].Rows[0].ItemArray);
// }
}
XmlNodeReader reader = new XmlNodeReader(rootNode);
ds.ReadXml(reader);
return ds;
}

// === 更新结点/元素 ====== 更新结点/元素 ===#region === 更新结点/元素 ===

/**/
///
/// 更新一元素
///
///
"elementPath">结点地址
///
"content">值
public bool UpdateElement(
string elementPath,
string content)

...{
XmlNode xn = _objXmlDoc.SelectSingleNode(elementPath);
if (xn == null)
return false;
xn.InnerText = content;
return true;
}

/**/
///
/// 更新某一结点下的指定元素值
///
///
"nodePath">目标结点
///
"elements">元素名称
///
"cotents">元素新值
public bool UpdateElements(
string nodePath,
string[] elements,
string[] contents)

...{
XmlNode objNode = _objXmlDoc.SelectSingleNode(nodePath); // 找到目标结点
if (objNode == null)
return false;
// 更新目标结点的元素
for (int i = 0; i < elements.Length; i++)

...{
objNode.SelectSingleNode(elements[i]).InnerText = contents[i];
}
return true;
}

// === 删除结点/元素 ====== 删除结点/元素 ===#region === 删除结点/元素 ===

/**/
///
/// 删除一结点(包括子结点)
///
///
"nodePath">目标结点
public bool DeleteNode(
string nodePath)

...{
try

...{
XmlNode aimNode = _objXmlDoc.SelectSingleNode(nodePath); // 目标结点
if (aimNode != null)

...{
aimNode.ParentNode.RemoveChild(aimNode); // 删除
}
return true;
}
catch

...{
return false;
}
}

/**/
///
/// 删除符合条件的所有结点
///
///
"xPath">条件
public bool DeleteNodes(
string xPath)

...{
XmlNodeList listNode = _objXmlDoc.SelectNodes(xPath);
if (listNode == null)
return true;
try

...{
foreach (XmlNode aimNode in listNode)

...{
aimNode.ParentNode.RemoveChild(aimNode);
}
return true;
}
catch

...{
return false;
}
}

// === 插入结点/元素 ====== 插入结点/元素 ===#region === 插入结点/元素 ===

/**/
///
/// 插入一节点和其所有元素
///
///
"MainNode">父节点,目标位置
///
"ChildNodes">新的结点
///
"Element">新的元素名称
///
"Content">元素内容
public bool InsertNodeWithElements(
string MainNode,
string ChildNode,
string[] Elements,
string[] Contents)

...{
return InsertNodeWithElements(MainNode, ChildNode, Elements, Contents, true);
}
public bool InsertNodeWithElements(
string MainNode,
string ChildNode,
string[] Elements,
string[] Contents,
bool isLast)

...{
XmlNode objRootNode = _objXmlDoc.SelectSingleNode(MainNode); // 找到父节点,即目标位置
if (objRootNode != null)

...{
XmlElement objChildNode = _objXmlDoc.CreateElement(ChildNode); // 新建结点
if (isLast)

...{
objRootNode.AppendChild(objChildNode); // 添加到父结点末尾
}
else

...{
objRootNode.PrependChild(objChildNode); // 添加到父结点开头
}
// 添加所有元素及内容到新加结点中
for (int i = 0; i < Elements.Length; i++)

...{
XmlElement objElement = _objXmlDoc.CreateElement(Elements[i]);
objElement.InnerText = Contents[i];
objChildNode.AppendChild(objElement);
}
return true;
}
return false;
}

/**/
///
/// 在一位置插入多个元素
///
///
"MainNode">目标位置
///
"Elements">元素名
///
"Contents">内容
public bool InsertElements(
string MainNode,
string[] Elements,
string[] Contents)

...{
XmlNode objNode = _objXmlDoc.SelectSingleNode(MainNode); // 找到目标位置
if (objNode != null)

...{
// 添加所有元素和对应内容
for (int i = 0; i < Elements.Length; i++)

...{
XmlElement objElement = _objXmlDoc.CreateElement(Elements[i]);
objElement.InnerText = Contents[i];
objNode.AppendChild(objElement);
}
return true;
}
return false;
}

// === 保存 ====== 保存 ===#region === 保存 ===
public bool Save()

...{
bool re = false;
try

...{
_objXmlDoc.Save(_strXmlFile);
re = true;
}
catch (System.Exception ex)

...{
re = false;
throw ex;
}
finally

...{
_objXmlDoc = null;
}
return re;
}