///& lt;總結& gt
/// XML操作基類
///& lt;/summary & gt;
公共類XmlHelper
{
/**/
///& lt;總結& gt
///將Xml讀入數據集中。
///& lt;/summary & gt;
///& lt;param name = " XmlPath " & gt小路
///& lt;returns & gt結果集
公共靜態數據集GetXml(字符串XmlPath)
{
數據集ds =新數據集();
ds。read XML(@ XML path);
返回ds;
}
/**/
///& lt;總結& gt
///讀取xml文檔,返回壹個節點:適用於第壹級節點。
///& lt;/summary & gt;
///& lt;param name = " XmlPath " & gtXml路徑
///& lt;param name = " NodeName " & gt結節
///& lt;returns & gt& lt/returns & gt;
公共靜態字符串ReadXmlReturnNode(字符串XmlPath,字符串節點)
{
XML document doc XML = new XML document();
docXml。load(@ XML path);
XmlNodeList xn = docXml。GetElementsByTagName(節點);
返回xn。item(0). innertext . tostring();
}
/**/
///& lt;總結& gt
///查找數據,返回當前節點的所有下屬節點,填入壹個數據集中。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml文檔路徑
///& lt;param name = " XmlPathNode " & gt節點路徑:根節點/父節點/當前節點
///& lt;returns & gt& lt/returns & gt;
公共靜態數據集GetXmlData(字符串xmlPath,字符串XmlPathNode)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
數據集ds =新數據集();
string reader read = new string reader(objXmlDoc。SelectSingleNode(XmlPathNode)。outer XML);
ds。ReadXml(讀取);
返回ds;
}
/**/
///& lt;總結& gt
///更新Xml節點內容
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml路徑
///& lt;param name = " Node " & gt要更改內容的節點:節點路徑根節點/父節點/當前節點
///& lt;param name = " Content " & gt新內容
公共靜態void XmlNodeReplace(字符串xmlPath,字符串節點,字符串內容)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
objXmlDoc。SelectSingleNode(節點)。InnerText =內容;
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///更改節點的屬性值
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gt文件路徑
///& lt;param name = " NodePath " & gt節點路徑
///& lt;param name = " node attribute 1 " >要更改的節點屬性的名稱
///& lt;param name = " NodeAttributeText " & gt;屬性值已更改
public static void XmlAttributeEdit(字符串xmlPath,字符串節點路徑,字符串節點屬性1,字符串節點屬性文本)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XmlNode nodePath = objXmlDoc。SelectSingleNode(NodePath);
XML element xe =(XML element)nodePath;
xe。set attribute(nodeattribute 1,NodeAttributeText);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///刪除XML節點及其子節點。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml文檔路徑
///& lt;param name = " Node " & gt節點路徑
公共靜態void xmlnode delete(string XML path,string Node)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
字符串mainNode = Node。子串(0,節點。LastIndexOf("/");
objXmlDoc。SelectSingleNode(主節點)。RemoveChild(objXmlDoc。SelectSingleNode(Node));
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///刪除節點的屬性。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gt文件路徑
///& lt;param name = " NodePath " & gt節點路徑(XPath)
///& lt;param name = " NodeAttribute " & gt屬性名
公共靜態void xmlnNodeAttributeDel(字符串xmlPath,字符串NodePath,字符串NodeAttribute)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XmlNode nodePath = objXmlDoc。SelectSingleNode(NodePath);
XML element xe =(XML element)nodePath;
xe。remove attribute(node attribute);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///插入節點及其子節點。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml路徑
///& lt;param name = " MailNode " & gt當前節點路徑
///& lt;param name = " ChildNode " & gt新插入的節點
///& lt;param name = " Element " & gt插入節點的子節點
///& lt;param name = " Content " & gt子節點的內容
公共靜態void XmlInsertNode(字符串xmlPath、字符串MailNode、字符串ChildNode、字符串元素、字符串內容)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XmlNode objRootNode = objXmlDoc。SelectSingleNode(MailNode);
XML element objChildNode = objXmlDoc。CreateElement(child node);
objRootNode。AppendChild(objChildNode);
XmlElement objElement = objXmlDoc。CreateElement(元素);
對象元素。InnerText =內容;
objChildNode。AppendChild(obj element);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///向節點添加屬性
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml文件路徑
///& lt;param name = " NodePath " & gt節點路徑
///& lt;param name = " node attribute 1 " >要添加的節點屬性的名稱
///& lt;param name = " NodeAttributeText " & gt;要添加的屬性值
公共靜態void AddAttribute(string xmlPath,string NodePath,string NodeAttribute1,string NodeAttributeText)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XML attribute nodeAttribute = objxml doc。create attribute(nodeattribute 1);
XmlNode nodePath = objXmlDoc。SelectSingleNode(NodePath);
節點路徑。attributes . Append(node attribute);
XML element xe =(XML element)nodePath;
xe。set attribute(nodeattribute 1,NodeAttributeText);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///插入具有屬性的節點。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml文檔路徑
///& lt;param name = " MainNode " & gt當前節點路徑
///& lt;param name = " Element " & gt新節點
///& lt;param name = " Attrib " & gt屬性名
///& lt;param name = " AttribContent " & gt屬性值
///& lt;param name = " Content " & gt新節點值
公共靜態void XmlInsertElement(字符串xmlPath,字符串MainNode,字符串元素,字符串屬性,字符串屬性內容,字符串內容)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XmlNode objNode = objXmlDoc。SelectSingleNode(主節點);
XmlElement objElement = objXmlDoc。CreateElement(元素);
對象元素。SetAttribute(Attrib,Attrib content);
對象元素。InnerText =內容;
objNode。AppendChild(obj element);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///插入沒有屬性的節點。
///& lt;/summary & gt;
///& lt;param name = " xmlPath " & gtXml文檔路徑
///& lt;param name = " MainNode " & gt當前節點路徑
///& lt;param name = " Element " & gt新節點
///& lt;param name = " Content " & gt新節點值
公共靜態void XmlInsertElement(字符串xmlPath,字符串MainNode,字符串元素,字符串內容)
{
XML document objXmlDoc = new XML document();
objXmlDoc。load(XML path);
XmlNode objNode = objXmlDoc。SelectSingleNode(主節點);
XmlElement objElement = objXmlDoc。CreateElement(元素);
對象元素。InnerText =內容;
objNode。AppendChild(obj element);
objXmlDoc。save(XML path);
}
/**/
///& lt;總結& gt
///在根節點下添加父節點
///& lt;/summary & gt;
公共靜態void AddParentNode(字符串xmlPath,字符串ParentNode)
{
XML document xdoc = new XML document();
xdoc。load(XML path);
//創建壹個新的成員節點,並將其添加到根節點。
XmlElement Node = xdoc。CreateElement(parent node);
xdoc。DocumentElement.PrependChild(節點);
xdoc。save(XML path);
}
/**/
///& lt;總結& gt
///根據節點屬性讀取子節點值(資源節約模式)
///& lt;/summary & gt;
///& lt;param name = " XmlPath " & gtXml路徑
///& lt;param name = " FatherElement " & gt父節點值
///& lt;param name = " AttributeName " & gt屬性名
///& lt;param name = " AttributeValue " & gt屬性值
///& lt;param name = " ArrayLength " & gt返回的數組長度
///& lt;returns & gt& lt/returns & gt;
公共靜態系統。collections . ArrayList GetSubElementByAttribute(string XML path,string FatherElement,string AttributeName,string AttributeValue,int ArrayLength)
{
系統。數組列表al =新系統。collections . ArrayList();
XML document doc XML = new XML document();
docXml。load(@ XML path);
XmlNodeList xn
xn = docXml。document element . select nodes("//"+father element+"["+@ AttributeName+" = ' "+attribute value+" ']");
XmlNodeList xx = xn。項目(0)。子節點;
for(int I = 0;我& lt數組長度& amp我& ltxx。數數;i++)
{
艾爾。添加(xx。項目(壹)。InnerText);
}
返回al;
}