/**********************************************************************************
*
* 功能說(shuō)明:XML處理基類(lèi)
* 作者: 劉功勛;
* 版本:V0.1(C#2.0);時(shí)間:2006-12-13
*
* *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
using MSXML2;
namespace EC
{
/// <summary>
/// XML 操作基類(lèi)
/// </summary>
public class XmlObject : IDisposable
{
//以下為單一功能的靜態(tài)類(lèi)
#region 讀取XML到DataSet
/**************************************************
* 函數(shù)名稱(chēng):GetXml(string XmlPath)
* 功能說(shuō)明:讀取XML到DataSet
* 參 數(shù):XmlPath:xml文檔路徑
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲取xml路徑
* DataSet ds = EC.XmlObject.GetXml(xmlPath); //讀取xml到DataSet中
************************************************/
/// <summary>
/// 功能:讀取XML到DataSet中
/// </summary>
/// <param name="XmlPath">xml路徑</param>
/// <returns>DataSet</returns>
public static DataSet GetXml(string XmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@XmlPath);
return ds;
}
#endregion
#region 讀取xml文檔并返回一個(gè)節(jié)點(diǎn)
/**************************************************
* 函數(shù)名稱(chēng):ReadXmlReturnNode(string XmlPath,string Node)
* 功能說(shuō)明:讀取xml文檔并返回一個(gè)節(jié)點(diǎn):適用于一級(jí)節(jié)點(diǎn)
* 參 數(shù): XmlPath:xml文檔路徑;Node 返回的節(jié)點(diǎn)名稱(chēng)
* 適應(yīng)用Xml:<?xml version="1.0" encoding="utf-8" ?>
* <root>
* <dns1>ns1.everdns.com</dns1>
* </root>
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲取xml路徑
* Response.Write(XmlObject.ReadXmlReturnNode(xmlPath, "mailmanager"));
************************************************/
/// <summary>
/// 讀取xml文檔并返回一個(gè)節(jié)點(diǎn):適用于一級(jí)節(jié)點(diǎn)
/// </summary>
/// <param name="XmlPath">xml路徑</param>
/// <param name="NodeName">節(jié)點(diǎn)</param>
/// <returns></returns>
public static string ReadXmlReturnNode(string XmlPath,string Node)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn = docXml.GetElementsByTagName(Node);
return xn.Item(0).InnerText.ToString();
}
#endregion
#region 查找數(shù)據(jù),返回一個(gè)DataSet
/**************************************************
* 函數(shù)名稱(chēng):GetXmlData(string xmlPath, string XmlPathNode)
* 功能說(shuō)明:查找數(shù)據(jù),返回當(dāng)前節(jié)點(diǎn)的所有下級(jí)節(jié)點(diǎn),填充到一個(gè)DataSet中
* 參 數(shù):xmlPath:xml文檔路徑;XmlPathNode:當(dāng)前節(jié)點(diǎn)的路徑
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* DataSet ds = new DataSet();
* ds = XmlObject.GetXmlData(xmlPath, "root/items");//讀取當(dāng)前路徑
* this.GridView1.DataSource = ds;
* this.GridView1.DataBind();
* ds.Clear();
* ds.Dispose();
* Xml示例:
* <?xml version="1.0" encoding="utf-8" ?>
* <root>
* <items name="xinnet">
* <url></url>
* <port>80</port>
* </items>
* </root>
************************************************/
/// <summary>
/// 查找數(shù)據(jù),返回當(dāng)前節(jié)點(diǎn)的所有下級(jí)節(jié)點(diǎn),填充到一個(gè)DataSet中
/// </summary>
/// <param name="xmlPath">xml文檔路徑</param>
/// <param name="XmlPathNode">節(jié)點(diǎn)的路徑:根節(jié)點(diǎn)/父節(jié)點(diǎn)/當(dāng)前節(jié)點(diǎn)</param>
/// <returns></returns>
public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds;
}
#endregion
#region 更新Xml節(jié)點(diǎn)內(nèi)容
/**************************************************
* 函數(shù)名稱(chēng):XmlNodeReplace(string xmlPath,string Node,string Content)
* 功能說(shuō)明:更新Xml節(jié)點(diǎn)內(nèi)容
* 參 數(shù):xmlPath:xml文檔路徑;Node:當(dāng)前節(jié)點(diǎn)的路徑;Content:內(nèi)容
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* XmlObject.XmlNodeReplace(xmlPath, "root/test", "56789"); //更新節(jié)點(diǎn)內(nèi)容
************************************************/
/// <summary>
/// 更新Xml節(jié)點(diǎn)內(nèi)容
/// </summary>
/// <param name="xmlPath">xml路徑</param>
/// <param name="Node">要更換內(nèi)容的節(jié)點(diǎn):節(jié)點(diǎn)路徑 根節(jié)點(diǎn)/父節(jié)點(diǎn)/當(dāng)前節(jié)點(diǎn)</param>
/// <param name="Content">新的內(nèi)容</param>
public static void XmlNodeReplace(string xmlPath,string Node,string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
objXmlDoc.SelectSingleNode(Node).InnerText = Content;
objXmlDoc.Save(xmlPath);
}
#endregion
#region 刪除XML節(jié)點(diǎn)和此節(jié)點(diǎn)下的子節(jié)點(diǎn)
/**************************************************
* 函數(shù)名稱(chēng):XmlNodeDelete(string xmlPath,string Node)
* 功能說(shuō)明:刪除XML節(jié)點(diǎn)和此節(jié)點(diǎn)下的子節(jié)點(diǎn)
* 參 數(shù):xmlPath:xml文檔路徑;Node:當(dāng)前節(jié)點(diǎn)的路徑;
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* XmlObject.XmlNodeDelete(xmlPath, "root/test"); //刪除當(dāng)前節(jié)點(diǎn)
************************************************/
/// <summary>
/// 刪除XML節(jié)點(diǎn)和此節(jié)點(diǎn)下的子節(jié)點(diǎn)
/// </summary>
/// <param name="xmlPath">xml文檔路徑</param>
/// <param name="Node">節(jié)點(diǎn)路徑</param>
public static void XmlNodeDelete(string xmlPath,string Node)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
string mainNode = Node.Substring(0, Node.LastIndexOf("http://m.survivalescaperooms.com/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
objXmlDoc.Save(xmlPath);
}
#endregion
#region 插入一個(gè)節(jié)點(diǎn)和此節(jié)點(diǎn)的字節(jié)點(diǎn)
/**************************************************
* 函數(shù)名稱(chēng):XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
* 功能說(shuō)明:插入一個(gè)節(jié)點(diǎn)和此節(jié)點(diǎn)的字節(jié)點(diǎn)
* 參 數(shù):xmlPath:xml文檔路徑;MailNode:當(dāng)前節(jié)點(diǎn)的路徑;ChildNode:新插入的節(jié)點(diǎn);Element:插入節(jié)點(diǎn)的子節(jié)點(diǎn);Content:子節(jié)點(diǎn)的內(nèi)容
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* XmlObject.XmlInsertNode(xmlPath, "root/test","test1","test2","測(cè)試內(nèi)容"); //插入一個(gè)節(jié)點(diǎn)和此節(jié)點(diǎn)的字節(jié)點(diǎn)
* 生成的XML格式為
* <test>
* <test1>
* <test2>測(cè)試內(nèi)容</test2>
* </test1>
* </test>
************************************************/
/// <summary>
/// 插入一個(gè)節(jié)點(diǎn)和此節(jié)點(diǎn)的字節(jié)點(diǎn)
/// </summary>
/// <param name="xmlPath">xml路徑</param>
/// <param name="MailNode">當(dāng)前節(jié)點(diǎn)路徑</param>
/// <param name="ChildNode">新插入節(jié)點(diǎn)</param>
/// <param name="Element">插入節(jié)點(diǎn)的子節(jié)點(diǎn)</param>
/// <param name="Content">子節(jié)點(diǎn)的內(nèi)容</param>
public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
#region 插入一節(jié)點(diǎn),帶一屬性
/**************************************************
* 函數(shù)名稱(chēng):XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
* 功能說(shuō)明:插入一節(jié)點(diǎn),帶一屬性
* 參 數(shù):xmlPath:xml文檔路徑;MailNode:當(dāng)前節(jié)點(diǎn)的路徑;Element:新插入的節(jié)點(diǎn);Attrib:屬性名稱(chēng);AttribContent:屬性值;Content:節(jié)點(diǎn)的內(nèi)容
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* XmlObject.XmlInsertElement(xmlPath, "root/test", "test1", "Attrib", "屬性值", "節(jié)點(diǎn)內(nèi)容"); //插入一節(jié)點(diǎn),帶一屬性
* 生成的XML格式為
* <test>
* <test1 Attrib="屬性值">節(jié)點(diǎn)內(nèi)容</test1>
* </test>
************************************************/
/// <summary>
/// 插入一節(jié)點(diǎn),帶一屬性
/// </summary>
/// <param name="xmlPath">Xml文檔路徑</param>
/// <param name="MainNode">當(dāng)前節(jié)點(diǎn)路徑</param>
/// <param name="Element">新節(jié)點(diǎn)</param>
/// <param name="Attrib">屬性名稱(chēng)</param>
/// <param name="AttribContent">屬性值</param>
/// <param name="Content">新節(jié)點(diǎn)值</param>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
#region 插入一節(jié)點(diǎn)不帶屬性
/**************************************************
* 函數(shù)名稱(chēng):XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
* 功能說(shuō)明:插入一節(jié)點(diǎn)不帶屬性
* 參 數(shù):xmlPath:xml文檔路徑;MailNode:當(dāng)前節(jié)點(diǎn)的路徑;Element:新插入的節(jié)點(diǎn);Content:節(jié)點(diǎn)的內(nèi)容
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑
* XmlObject.XmlInsertElement(xmlPath, "root/test", "text1", "節(jié)點(diǎn)內(nèi)容"); //插入一節(jié)點(diǎn)不帶屬性
* 生成的XML格式為
* <test>
* <text1>節(jié)點(diǎn)內(nèi)容</text1>
* </test>
************************************************/
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
#endregion
//必須創(chuàng)建對(duì)象才能使用的類(lèi)
private bool _alreadyDispose = false;
private string xmlPath;
private XmlDocument xmlDoc=new XmlDocument();
private XmlNode xmlNode;
private XmlElement xmlElem;
#region 構(gòu)造與釋構(gòu)
public XmlObject()
{
}
~XmlObject()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
//
}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 創(chuàng)建xml文檔
/**************************************************
* 對(duì)象名稱(chēng):XmlObject
* 功能說(shuō)明:創(chuàng)建xml文檔
* 使用示列:
* using EC; //引用命名空間
* string xmlPath = Server.MapPath("test.xml");
* XmlObject obj = new XmlObject();
* 創(chuàng)建根節(jié)點(diǎn)
* obj.CreateXmlRoot("root");
* // 創(chuàng)建空節(jié)點(diǎn)
* //obj.CreatXmlNode("root", "Node");
* //創(chuàng)建一個(gè)帶值的節(jié)點(diǎn)
* //obj.CreatXmlNode("root", "Node", "帶值的節(jié)點(diǎn)");
* //創(chuàng)建一個(gè)僅帶屬性的節(jié)點(diǎn)
* //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值");
* //創(chuàng)建一個(gè)僅帶兩個(gè)屬性值的節(jié)點(diǎn)
* //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2");
* //創(chuàng)建一個(gè)帶屬性值的節(jié)點(diǎn)值的節(jié)點(diǎn)
* // obj.CreatXmlNode("root", "Node", "Attribe", "屬性值","節(jié)點(diǎn)值");
* //在當(dāng)前節(jié)點(diǎn)插入帶兩個(gè)屬性值的節(jié)點(diǎn)
* obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2","節(jié)點(diǎn)值");
* obj.XmlSave(xmlPath);
* obj.Dispose();
************************************************/
#region 創(chuàng)建一個(gè)只有聲明和根節(jié)點(diǎn)的XML文檔
/// <summary>
/// 創(chuàng)建一個(gè)只有聲明和根節(jié)點(diǎn)的XML文檔
/// </summary>
/// <param name="root"></param>
public void CreateXmlRoot(string root)
{
//加入XML的聲明段落
xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlDoc.AppendChild(xmlNode);
//加入一個(gè)根元素
xmlElem = xmlDoc.CreateElement("", root, "");
xmlDoc.AppendChild(xmlElem);
}
#endregion
#region 在當(dāng)前節(jié)點(diǎn)下插入一個(gè)空節(jié)點(diǎn)節(jié)點(diǎn)
/// <summary>
/// 在當(dāng)前節(jié)點(diǎn)下插入一個(gè)空節(jié)點(diǎn)節(jié)點(diǎn)
/// </summary>
/// <param name="mainNode">當(dāng)前節(jié)點(diǎn)路徑</param>
/// <param name="node">節(jié)點(diǎn)名稱(chēng)</param>
public void CreatXmlNode(string mainNode,string node)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
MainNode.AppendChild(objElem);
}
#endregion
#region 在當(dāng)前節(jié)點(diǎn)插入一個(gè)僅帶值的節(jié)點(diǎn)
/// <summary>
/// 在當(dāng)前節(jié)點(diǎn)插入一個(gè)僅帶值的節(jié)點(diǎn)
/// </summary>
/// <param name="mainNode">當(dāng)前節(jié)點(diǎn)</param>
/// <param name="node">新節(jié)點(diǎn)</param>
/// <param name="content">新節(jié)點(diǎn)值</param>
public void CreatXmlNode(string mainNode, string node, string content)
{
XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
XmlElement objElem = xmlDoc.CreateElement(node);
objElem.InnerText = content;
MainNode.AppendChild(objElem);
}
#endregion
#region 在當(dāng)前節(jié)點(diǎn)的插入一個(gè)僅帶屬性值的節(jié)點(diǎn)
/// <summary>
/// 在當(dāng)前節(jié)點(diǎn)的插入一個(gè)僅帶屬性值的節(jié)點(diǎn)
/// </summary>
/// <param name="MainNode">當(dāng)前節(jié)點(diǎn)或路徑</param>
/// <param name="Node">新節(jié)點(diǎn)</param>
/// <param name="Attrib">新節(jié)點(diǎn)屬性名稱(chēng)</param>
/// <param name="AttribValue">新節(jié)點(diǎn)屬性值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
mainNode.AppendChild(objElem);
}
#endregion
#region 創(chuàng)建一個(gè)帶屬性值的節(jié)點(diǎn)值的節(jié)點(diǎn)
/// <summary>
/// 創(chuàng)建一個(gè)帶屬性值的節(jié)點(diǎn)值的節(jié)點(diǎn)
/// </summary>
/// <param name="MainNode">當(dāng)前節(jié)點(diǎn)或路徑</param>
/// <param name="Node">節(jié)點(diǎn)名稱(chēng)</param>
/// <param name="Attrib">屬性名稱(chēng)</param>
/// <param name="AttribValue">屬性值</param>
/// <param name="Content">節(jié)點(diǎn)傳情</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue,string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
#region 在當(dāng)前節(jié)點(diǎn)的插入一個(gè)僅帶2個(gè)屬性值的節(jié)點(diǎn)
/// <summary>
/// 在當(dāng)前節(jié)點(diǎn)的插入一個(gè)僅帶2個(gè)屬性值的節(jié)點(diǎn)
/// </summary>
/// <param name="MainNode">當(dāng)前節(jié)點(diǎn)或路徑</param>
/// <param name="Node">節(jié)點(diǎn)名稱(chēng)</param>
/// <param name="Attrib">屬性名稱(chēng)一</param>
/// <param name="AttribValue">屬性值一</param>
/// <param name="Attrib2">屬性名稱(chēng)二</param>
/// <param name="AttribValue2">屬性值二</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue,string Attrib2,string AttribValue2)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
mainNode.AppendChild(objElem);
}
#endregion
#region 在當(dāng)前節(jié)點(diǎn)插入帶兩個(gè)屬性的節(jié)點(diǎn)
/// <summary>
/// 在當(dāng)前節(jié)點(diǎn)插入帶兩個(gè)屬性的節(jié)點(diǎn)
/// </summary>
/// <param name="MainNode">當(dāng)前節(jié)點(diǎn)或路徑</param>
/// <param name="Node">節(jié)點(diǎn)名稱(chēng)</param>
/// <param name="Attrib">屬性名稱(chēng)一</param>
/// <param name="AttribValue">屬性值一</param>
/// <param name="Attrib2">屬性名稱(chēng)二</param>
/// <param name="AttribValue2">屬性值二</param>
/// <param name="Content">節(jié)點(diǎn)值</param>
public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2,string Content)
{
XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
XmlElement objElem = xmlDoc.CreateElement(Node);
objElem.SetAttribute(Attrib, AttribValue);
objElem.SetAttribute(Attrib2, AttribValue2);
objElem.InnerText = Content;
mainNode.AppendChild(objElem);
}
#endregion
#region 保存Xml
/// <summary>
/// 保存Xml
/// </summary>
/// <param name="path">保存的當(dāng)前路徑</param>
public void XmlSave(string path)
{
xmlDoc.Save(path);
}
#endregion
#endregion
#region 根據(jù)父節(jié)點(diǎn)屬性值讀取子節(jié)點(diǎn)值
/**************************************************
* 函數(shù)名稱(chēng):GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength)
* 功能說(shuō)明:根據(jù)父節(jié)點(diǎn)屬性值讀取子節(jié)點(diǎn)值
* 參 數(shù): XmlPath:xml路徑;FatherElenetName:父節(jié)點(diǎn)名;AttributeName:屬性值;AttributeIndex:屬性索引;ArrayLength:要返回的節(jié)點(diǎn)數(shù)組長(zhǎng)度
* 適應(yīng)用Xml:
* <root>
* <page name="/index.aspx">
* <title>域名注冊(cè)、虛擬主機(jī)、企業(yè)郵局、服務(wù)器托管、網(wǎng)站空間租用|---第一商務(wù)</title>
* <keywords>虛擬主機(jī),域名注冊(cè),服務(wù)器托管,杭州,服務(wù)器租用,</keywords>
* <description>描述內(nèi)容 </description>
* </page>
* </root>
* ArrayList al = new ArrayList();
* al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "/index.aspx", 0, 3);
* for (int i = 0; i < al.Count; i++)
* {
* Response.Write(al[i].ToString());
* Response.Write("<br>");
* }
************************************************/
/// <summary>
/// 根據(jù)父節(jié)點(diǎn)屬性讀取字節(jié)點(diǎn)值
/// </summary>
/// <param name="XmlPath">xml路徑</param>
/// <param name="FatherElenetName">父節(jié)點(diǎn)名</param>
/// <param name="AttributeName">屬性值</param>
/// <param name="AttributeIndex">屬性索引</param>
/// <param name="ArrayLength">要返回的節(jié)點(diǎn)數(shù)組長(zhǎng)度</param>
/// <returns></returns>
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn = docXml.DocumentElement.ChildNodes;
//遍歷第一層節(jié)點(diǎn)
foreach (XmlElement element in xn)
{
//判斷父節(jié)點(diǎn)是否為指定節(jié)點(diǎn)
if (element.Name == FatherElenetName)
{
//判斷父節(jié)點(diǎn)屬性的索引是否大于指定索引
if (element.Attributes.Count < AttributeIndex)
return null;
//判斷父節(jié)點(diǎn)的屬性值是否等于指定屬性
if (element.Attributes[AttributeIndex].Value == AttributeName)
{
XmlNodeList xx = element.ChildNodes;
if (xx.Count > 0)
{
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{
al.Add(xx[i].InnerText);
}
}
}
}
}
return al;
}
#endregion
#region 根據(jù)節(jié)點(diǎn)屬性讀取子節(jié)點(diǎn)值(較省資源模式)
/**************************************************
* 函數(shù)名稱(chēng):GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
* 功能說(shuō)明:根據(jù)父節(jié)點(diǎn)屬性值讀取子節(jié)點(diǎn)值
* 參 數(shù): XmlPath:xml路徑;FatherElenetName:父節(jié)點(diǎn)名;AttributeName:屬性名;AttributeValue:屬性值;ArrayLength:要返回的節(jié)點(diǎn)數(shù)組長(zhǎng)度
* 適應(yīng)用Xml:
* <root>
* <page name="/index.aspx">
* <title>域名注冊(cè)、虛擬主機(jī)、企業(yè)郵局、服務(wù)器托管、網(wǎng)站空間租用|---第一商務(wù)</title>
* <keywords>虛擬主機(jī),域名注冊(cè),服務(wù)器托管,杭州,服務(wù)器租用,</keywords>
* <description>描述內(nèi)容 </description>
* </page>
* </root>
* ArrayList al = new ArrayList();
* al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "@name", "/index.aspx", 3);
* for (int i = 0; i < al.Count; i++)
* {
* Response.Write(al[i].ToString());
* Response.Write("<br>");
* }
************************************************/
/// <summary>
/// 根據(jù)節(jié)點(diǎn)屬性讀取子節(jié)點(diǎn)值(較省資源模式)
/// </summary>
/// <param name="XmlPath">xml路徑</param>
/// <param name="FatherElement">父節(jié)點(diǎn)值</param>
/// <param name="AttributeName">屬性名稱(chēng)</param>
/// <param name="AttributeValue">屬性值</param>
/// <param name="ArrayLength">返回的數(shù)組長(zhǎng)度</param>
/// <returns></returns>
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn;
xn = docXml.DocumentElement.SelectNodes("http://" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
XmlNodeList xx = xn.Item(0).ChildNodes;
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{
al.Add(xx.Item(i).InnerText);
}
return al;
}
#endregion
}
}