XML操作

public class XMLOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof);
Message mesg = null;
XmlDocument doc;
public XMLOperation(string fileName)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Collections;

C#创建、读写、增加、删除XML操作,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Collections;

namespace CommandXML
{
    public class cmdXML
    {

        /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name=”xmlFilePath”>存放目录</param>
        /// <param name=”rootNodename”>根节点名字</param>
        public void CreateXMLDoc(string xmlFilePath, string
rootNodename)
         {
             //开始化多少个xml实例
             XmlDocument myXmlDoc = new XmlDocument();
             //<?xml version=”1.0″ encoding=”UTF-8″?>
             myXmlDoc.AppendChild(myXmlDoc.CreateXmlDeclaration(“1.0”,
“UTF-8”, null));
             //创建xml的根节点
             XmlElement rootElement =
myXmlDoc.CreateElement(rootNodename);
             //将根节点加入到xml文件中(AppendChild)
             myXmlDoc.AppendChild(rootElement);
             myXmlDoc.Save(xmlFilePath);
         }

        /// <summary>
        /// 扩张第一层的节点
        /// </summary>
        /// <param name=”xmlFilePath”>文件路线</param>
        /// <param name=”RootPath”>根节点名字</param>
        /// <param
name=”Name”>所要增多第一层节点的节点名</param>
        /// <param name=”attribute”></param>
        public void AddXmlFirstNode(string xmlFilePath, string RootPath,
string Name, string[,] attribute)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

                 XmlNode memberlist =
myXmlDoc.SelectSingleNode(RootPath);
                 //XmlNodeList nodelist = memberlist.ChildNodes;

                 XmlElement firstLevelElement1 =
myXmlDoc.CreateElement(Name);
                 //填充第一层的首先个头节点的属性值(SetAttribute)
                 for (int i = 0; i < attribute.GetLength(0); i++)
                 {
                     firstLevelElement1.SetAttribute(attribute[i, 0],
attribute[i, 1]);
                 }
                 //将第一层的率先个子节点加入到根节点下
                 memberlist.AppendChild(firstLevelElement1);

                 //保存改变
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         /// <summary>
         /// 扩展第二层节点
         /// </summary>
         /// <param name=”xmlFilePath”>路径</param>
         /// <param name=”RootPath”>根节点名</param>
         /// <param
name=”FirstElementattributesName”>第一层节点属性名</param>
         /// <param
name=”Firstattributes”>第一层节点属性名对应的值</param>
         /// <param
name=”SecondElement”>所要增加的第二层节点名</param>
         /// <param
name=”SecondinnerText”>第二层节点对应的储存内容</param>
        public void AddXmlSecondNod(string xmlFilePath, string RootPath,
string FirstElementattributesName, string Firstattributes, string[]
SecondElement, string[] SecondinnerText)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

                 XmlNode memberlist =
myXmlDoc.SelectSingleNode(RootPath);
                 XmlNodeList nodelist = memberlist.ChildNodes;

                 //增多八个包罗属性的节点音讯
                 foreach (XmlNode node in nodelist)
                 {
                     if
(node.Attributes[FirstElementattributesName].Value.Equals(Firstattributes))
                     {
                         for (int i = 0; i < SecondElement.Length;
i++)
                         {
                             XmlElement newElement =
myXmlDoc.CreateElement(SecondElement[i]);
                             newElement.InnerText =
SecondinnerText[i];
                             node.AppendChild(newElement);
                         }
                     }
                 }
                 //保存更换
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

        /// <summary>
        /// 获取第一层节点的属性值,重返全体的属性名和对应的值
        /// </summary>
        /// <param name=”xmlFile帕特h”>文件路线</param>
        /// <param name=”Root帕特h”>根节点名</param>
        /// <param
name=”firstNodeName”>第一层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLFirstNodeAttributes(string xmlFilePath,
string RootPath, string firstNodeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //开头化三个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的路线)
                 myXmlDoc.Load(xmlFilePath);
                
//得到第多个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 //分获该节点的InnerXml和OuterXml消息
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的首先层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //获得该节点的性质集合
                     if (node.Name == firstNodeName)
                     {
                         XmlAttributeCollection attributeCol =
node.Attributes;
                         foreach (XmlAttribute attri in attributeCol)
                         {
                             //获取属性名称与属性值
                             string name = attri.Name;
                             string value = attri.Value;
                             list.Add(name + “:” + value);
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }

        /// <summary>
        /// 获取第二层节点的存储值
        /// </summary>
        /// <param name=”xmlFile帕特h”>文件路线</param>
        /// <param name=”RootPath”>根节点</param>
        /// <param
name=”firstNodeName”>第一层节点名</param>
        /// <param
name=”secondNoadeName”>第二层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLSecondNodeValue(string xmlFilePath,
string RootPath, string firstNodeName, string secondNoadeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //初叶化二个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的不二秘籍)
                 myXmlDoc.Load(xmlFilePath);
                
//获得第三个姓名相配的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 //分别获得该节点的InnerXml和OuterXml音讯
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的第一层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //获得该节点的属性集结
                     if (node.Name == firstNodeName)
                     {
                         foreach (XmlNode _node in node.ChildNodes)
                         {
                             if (_node.Name == secondNoadeName)
                                 list.Add(_node.InnerText);
                         }
                     }

                     //判定此节点是不是还会有子节点
                     if (node.HasChildNodes)
                     {
                         //获取该节点的首先身长节点
                         XmlNode secondLevelNode1 = node.FirstChild;
                         //获取该节点的名字
                         string name = secondLevelNode1.Name;
                         //获取该节点的值(即:InnerText)
                         string innerText =
secondLevelNode1.InnerText;
                         Console.WriteLine(“{0} = {1}”, name,
innerText);

                         //获取该节点的第叁个子节点(用数组下标获取)
                         XmlNode secondLevelNode2 =
node.ChildNodes[1];
                         name = secondLevelNode2.Name;
                         innerText = secondLevelNode2.InnerText;
                         Console.WriteLine(“{0} = {1}”, name,
innerText);
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }

        /// <summary>
        /// 修改第一层节点的属性值
        /// </summary>
        /// <param name=”xmlFilePath”>文件路径</param>
        /// <param name=”RootPath”>根节点名</param>
        /// <param name=”FirstNodeName”>第二节点名</param>
        /// <param
name=”FirstNodeAttributes”>第1节点属性名</param>
        /// <param
name=”FirstNodeAttributesOldValue”>第4节点属性值</param>
        /// <param name=”newValue”></param>
        public void ModifyXmlFirstattribute(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesOldValue, string newValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 //XmlNode rootNode = myXmlDoc.FirstChild;
                 //XmlNodeList firstLevelNodeList =
rootNode.ChildNodes;
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesOldValue))
                         {
                            
node.Attributes[FirstNodeAttributes].Value = newValue;
                         }
                     }
                 }
                 //要想使对xml文件所做的更换生效,必需实行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

       
        /// <summary>
        /// 修改第三节点的存款和储蓄值
        /// </summary>
        /// <param name=”xmlFile帕特h”>文件路线</param>
        /// <param name=”RootPath”>根节点名字</param>
        /// <param
name=”FirstNodeName”>第3节点名字</param>
        /// <param
name=”FirstNodeAttributes”>第3节点属性名</param>
        /// <param
name=”FirstNodeAttributesValue”>第2节点属性值</param>
        /// <param
name=”SecondNodeName”>第3节点名字</param>
        /// <param name=”value”>第一节点存款和储蓄值</param>
        public void ModifyXmlElementValue(string xmlFilePath, string
RootPath, string FirstNodeName,string FirstNodeAttributes, string
FirstNodeAttributesValue, string SecondNodeName, string value)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in
node.ChildNodes)
                             {
                                 if (_node.Name == SecondNodeName)
                                 {
                                     _node.InnerText = value;
                                 }
                             }
                         }
                     }
                 }
                 //要想使对xml文件所做的退换生效,必需进行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

 

        /// <summary>
        /// 删除首节点
        /// </summary>
        /// <param name=”xmlFilePath”>路径</param>
        /// <param name=”RootPath”>根节点</param>
        /// <param name=”FirstNodeName”>第3节点名</param>
        /// <param
name=”FirstNodeAttributes”>第4节点属性名</param>
        /// <param
name=”FirstNodeAttributesValue”>第4节点属性值</param>
        public void DeleteXmlFirstnode(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
XML操作。                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             //node.RemoveAll();
                             rootNode.RemoveChild(node);
                         }
                     }
                 }
                 //保存对xml文件所做的更换
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

        /// <summary>
         /// 删除子节点
        /// </summary>
        /// <param name=”xmlFilePath”>路径</param>
        /// <param
name=”FirstElementattributesName”>第1节点属性名</param>
        /// <param
name=”Firstattributes”>第二节点属性值</param>
        /// <param
name=”secondnodeName”>子节点名称</param>
        public void DeleteXmlsecondNode(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesValue, string secondnodeName)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in
node.ChildNodes)
                             {
                                 if (_node.Name == secondnodeName)
                                     //_node.RemoveAll();
                                     node.RemoveChild(_node);

                             }
                         }
                     }
                 }
                 //保存对xml文件所做的修改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

    }
}

using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Xml…

{
mesg = new Message();
try
{
doc = new XmlDocument();
doc.Load;
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
}
public XMLOperation()
{
mesg = new Message();
try
{
doc = new XmlDocument();
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
}

 

namespace CommandXML
{
    public class cmdXML
    {

public void CreateRootNode(string name)
{
try
{
if (name == “” || name.Trim().Length == 0)
{
return;
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

        /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name=”xmlFilePath”>寄放目录</param>
        /// <param name=”rootNodename”>根节点名字</param>
        public void CreateXMLDoc(string xmlFilePath, string
rootNodename)
         {
             //最初化一个xml实例
             XmlDocument myXmlDoc = new XmlDocument();
             //<?xml version=”1.0″ encoding=”UTF-8″?>
             myXmlDoc.AppendChild(myXmlDoc.CreateXmlDeclaration(“1.0”,
“UTF-8”, null));
             //创制xml的根节点
             XmlElement rootElement =
myXmlDoc.CreateElement(rootNodename);
             //将根节点加入到xml文件中(AppendChild)
             myXmlDoc.AppendChild(rootElement);
             myXmlDoc.Save(xmlFilePath);
         }

XmlDeclaration dec = doc.CreateXmlDeclaration(“1.0”, “utf-8”, null);
doc.AppendChild;
XmlNode node = doc.CreateNode(XmlNodeType.Element, name, “”);
doc.AppendChild;
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
}

namespace VSTestHelper
{
public class XMLHelper
{
public List<string> registryName = new List<string>();
public List<string> registryValue = new List<string>();
public string lastRegistry;
XmlDocument myXmlDoc = new XmlDocument();

        /// <summary>
        /// 扩大第一层的节点
        /// </summary>
        /// <param name=”xmlFilePath”>文件路线</param>
        /// <param name=”RootPath”>根节点名字</param>
        /// <param
name=”Name”>所要增加第一层节点的节点名</param>
        /// <param name=”attribute”></param>
        public void AddXmlFirstNode(string xmlFilePath, string RootPath,
string Name, string[,] attribute)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

public XmlNode AddNode(string pNodeName,string nodeName)
{
if (nodeName == null || nodeName.Length == 0)
{
return null;
}
XmlElement xe = null;

public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string
name, string value)
{
XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentNode.AppendChild(node);
}

                 XmlNode memberlist =
myXmlDoc.SelectSingleNode(RootPath);
                 //XmlNodeList nodelist = memberlist.ChildNodes;

try
{
XmlNode nd = doc.SelectSingleNode(pNodeName);
xe = doc.CreateElement;
nd.AppendChild;
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
return xe;

public void GenerateXMLFile(string xmlFilePath)
{
try
{
XmlElement rootElement = myXmlDoc.CreateElement(“Registry”);
myXmlDoc.AppendChild(rootElement);
XmlElement firstLevelElement1 =
myXmlDoc.CreateElement(“RegistryList”);
firstLevelElement1.SetAttribute(“Name”, “name”);
firstLevelElement1.SetAttribute(“Value”, “value”);
rootElement.AppendChild(firstLevelElement1);
myXmlDoc.Save(xmlFilePath);
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}

                 XmlElement firstLevelElement1 =
myXmlDoc.CreateElement(Name);
                 //填充第一层的第一身形节点的属性值(SetAttribute)
                 for (int i = 0; i < attribute.GetLength(0); i++)
                 {
                     firstLevelElement1.SetAttribute(attribute[i, 0],
attribute[i, 1]);
                 }
                 //将第一层的首先身形节点加入到根节点下
                 memberlist.AppendChild(firstLevelElement1);

}

public bool RegistryFile(string xmlFilePath)
{
bool _exist = false;
if (Directory.Exists(xmlFilePath))
{
Console.WriteLine(“The Registry Path has been existed!”);
_exist = true;
return _exist;
}
if(_exist==true){
GenerateXMLFile(xmlFilePath);
}
return _exist;
}

                 //保存更换
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

public void AddElement(XmlNode pNode, string elmName,string value)
{
try
{
XmlElement ele = doc.CreateElement;
ele.InnerText = value;
pNode.AppendChild;
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
}

public List<string> GetXMLInformation(string xmlFilePath)
{

         /// <summary>
         /// 扩大第二层节点
         /// </summary>
         /// <param name=”xmlFilePath”>路径</param>
         /// <param name=”RootPath”>根节点名</param>
         /// <param
name=”FirstElementattributesName”>第一层节点属性名</param>
         /// <param
name=”Firstattributes”>第一层节点属性名对应的值</param>
         /// <param
name=”SecondElement”>所要扩大的第二层节点名</param>
         /// <param
name=”SecondinnerText”>第二层节点对应的仓库储存内容</param>
        public void AddXmlSecondNod(string xmlFilePath, string RootPath,
string FirstElementattributesName, string Firstattributes, string[]
SecondElement, string[] SecondinnerText)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

public void SaveDoc(string fileName)
{
doc.Save;
}

try
{
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.SelectSingleNode(“Registry”);
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
if (node.HasChildNodes)
{
XmlNodeList secondLevelNode1 = node.ChildNodes;
foreach (XmlElement xmlElem in secondLevelNode1)
{
registryName.Add(xmlElem.Name);
registryValue.Add(xmlElem.InnerText);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
return registryName;
}

                 XmlNode memberlist =
myXmlDoc.SelectSingleNode(RootPath);
                 XmlNodeList nodelist = memberlist.ChildNodes;

public void ClearNodes(string rootNodeName)
{
try
{
XmlNode root = doc.SelectSingleNode(rootNodeName);
if (root != null)
{
root.RemoveAll();
}
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
}

public string GetLastXMLInformation(string xmlFilePath)
{
try
{
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.SelectSingleNode(“Registry”);
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
if (node.HasChildNodes)
{
XmlNode lastNode = node.LastChild;
string lastNodeName = lastNode.Name;
string lastNodeValue = lastNode.InnerText;
lastRegistry = lastNodeName + ” = ” + lastNodeValue;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
return lastRegistry;
}

                 //增多贰个含有属性的节点音信
                 foreach (XmlNode node in nodelist)
                 {
                     if
(node.Attributes[FirstElementattributesName].Value.Equals(Firstattributes))
                     {
                         for (int i = 0; i < SecondElement.Length;
i++)
                         {
                             XmlElement newElement =
myXmlDoc.CreateElement(SecondElement[i]);
                             newElement.InnerText =
SecondinnerText[i];
                             node.AppendChild(newElement);
                         }
                     }
                 }
                 //保存退换
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

public Dictionary<string, string> GetKeyValues( string
rootNodeName,string elmentKeyName,string elmentKeyValue)
{
Dictionary<string, string> dicRet = new Dictionary<string,
string>();
try
{
XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
if (rootNode.HasChildNodes)
{
XmlNodeList nodeList = rootNode.ChildNodes;
foreach (XmlNode node in nodeList)
{
if (node.HasChildNodes)
{
XmlNodeList subNodeList = node.ChildNodes;
bool bFind = false;
foreach (XmlNode subNode in subNodeList)
{
if (subNode.Name == elmentKeyName && subNode.InnerText ==
elmentKeyValue)
{
bFind = true;
dicRet.Add(subNode.Name, subNode.InnerText);
continue;
澳门葡京备用网址,}
if
{
dicRet.Add(subNode.Name, subNode.InnerText);
}
}
if
{
break;
}
}
}
}
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}

public void AddXmlInformation(string xmlFilePath, string name, string
value)
{
try
{
myXmlDoc.Load(xmlFilePath);
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
XmlElement newElement = myXmlDoc.CreateElement(name);
newElement.InnerText = value;
node.AppendChild(newElement);
}
myXmlDoc.Save(xmlFilePath);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}

        /// <summary>
        /// 获取第一层节点的属性值,再次来到全体的属性名和对应的值
        /// </summary>
        /// <param name=”xmlFile帕特h”>文件路径</param>
        /// <param name=”RootPath”>根节点名</param>
        /// <param
name=”firstNodeName”>第一层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLFirstNodeAttributes(string xmlFilePath,
string RootPath, string firstNodeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //伊始化一个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的路线)
                 myXmlDoc.Load(xmlFilePath);
                
//获得第三个姓名相配的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 //分别赢得该节点的InnerXml和OuterXml音信
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的率先层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //得到该节点的习性集结
                     if (node.Name == firstNodeName)
                     {
                         XmlAttributeCollection attributeCol =
node.Attributes;
                         foreach (XmlAttribute attri in attributeCol)
                         {
                             //获取属性名称与属性值
                             string name = attri.Name;
                             string value = attri.Value;
                             list.Add(name + “:” + value);
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }

return dicRet;
}

        /// <summary>
        /// 获取第二层节点的存储值
        /// </summary>
        /// <param name=”xmlFilePath”>文件路线</param>
        /// <param name=”RootPath”>根节点</param>
        /// <param
name=”firstNodeName”>第一层节点名</param>
        /// <param
name=”secondNoadeName”>第二层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLSecondNodeValue(string xmlFilePath,
string RootPath, string firstNodeName, string secondNoadeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //起始化五个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的必须要经过的路)
                 myXmlDoc.Load(xmlFilePath);
                
//得到第贰个姓名相称的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 //分别得到该节点的InnerXml和OuterXml消息
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的率先层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //获得该节点的质量集结
                     if (node.Name == firstNodeName)
                     {
                         foreach (XmlNode _node in node.ChildNodes)
                         {
                             if (_node.Name == secondNoadeName)
                                 list.Add(_node.InnerText);
                         }
                     }

public List<string> GetValues(string rootNodeName, string
elmentKeyName)
{
List<string> listRet =new List<string>();
try
{
XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
if (rootNode.HasChildNodes)
{
XmlNodeList nodeList = rootNode.ChildNodes;
foreach (XmlNode node in nodeList)
{
if (node.HasChildNodes)
{
XmlNodeList subNodeList = node.ChildNodes;
foreach (XmlNode subNode in subNodeList)
{
if (subNode.Name == elmentKeyName)
{
listRet.Add(subNode.InnerText);
break;
}
}
}
}
}
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}
return listRet;
}

                     //推断此节点是还是不是还会有子节点
                     if (node.HasChildNodes)
                     {
                         //获取该节点的第一身长节点
                         XmlNode secondLevelNode1 = node.FirstChild;
                         //获取该节点的名字
                         string name = secondLevelNode1.Name;
                         //获取该节点的值(即:InnerText)
                         string innerText =
secondLevelNode1.InnerText;
                         Console.WriteLine(“{0} = {1}”, name,
innerText);

public Dictionary<string, string> GetKeyValuesEx(string
rootNodeName, string atrributeName)
{
Dictionary<string, string> dicRet = new Dictionary<string,
string>();

                         //获取该节点的第二个子节点(用数组下标获取)
                         XmlNode secondLevelNode2 =
node.ChildNodes[1];
                         name = secondLevelNode2.Name;
                         innerText = secondLevelNode2.InnerText;
                         Console.WriteLine(“{0} = {1}”, name,
innerText);
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }

try
{
XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
if (rootNode.HasChildNodes)
{
XmlNodeList nodeList = rootNode.ChildNodes;
foreach (XmlNode node in nodeList)
{
XmlElement xe = (XmlElement)node;
string key = xe.GetAttribute(atrributeName);
if (!dicRet.ContainsKey
{
dicRet.Add(key, xe.InnerText);
}
}
}
}
catch (Exception ex)
{
Tools.LogMessage(log, mesg, ex.Message);
}

        /// <summary>
        /// 修改第一层节点的属性值
        /// </summary>
        /// <param name=”xmlFilePath”>文件路线</param>
        /// <param name=”RootPath”>根节点名</param>
        /// <param name=”FirstNodeName”>第三节点名</param>
        /// <param
name=”FirstNodeAttributes”>第2节点属性名</param>
        /// <param
name=”FirstNodeAttributesOldValue”>第三节点属性值</param>
        /// <param name=”newValue”></param>
        public void ModifyXmlFirstattribute(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesOldValue, string newValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 //XmlNode rootNode = myXmlDoc.FirstChild;
                 //XmlNodeList firstLevelNodeList =
rootNode.ChildNodes;
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesOldValue))
                         {
                            
node.Attributes[FirstNodeAttributes].Value = newValue;
                         }
                     }
                 }
                 //要想使对xml文件所做的改换生效,必需奉行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

return dicRet;
}

       
        /// <summary>
        /// 修改第一节点的存储值
        /// </summary>
        /// <param name=”xmlFilePath”>文件路线</param>
        /// <param name=”RootPath”>根节点名字</param>
        /// <param
name=”FirstNodeName”>第四节点名字</param>
        /// <param
name=”FirstNodeAttributes”>第二节点属性名</param>
        /// <param
name=”FirstNodeAttributesValue”>第2节点属性值</param>
        /// <param
name=”SecondNodeName”>第4节点名字</param>
        /// <param name=”value”>第3节点存款和储蓄值</param>
        public void ModifyXmlElementValue(string xmlFilePath, string
RootPath, string FirstNodeName,string FirstNodeAttributes, string
FirstNodeAttributesValue, string SecondNodeName, string value)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode =
myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in
node.ChildNodes)
                             {
                                 if (_node.Name == SecondNodeName)
                                 {
                                     _node.InnerText = value;
                                 }
                             }
                         }
                     }
                 }
                 //要想使对xml文件所做的修改生效,必需试行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

public void RemoveNode(string rootNodeName, string keyName,string
value)
{

 

XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
if (rootNode.HasChildNodes)
{
XmlNodeList nodeList = rootNode.ChildNodes;
foreach (XmlNode node in nodeList)
{
if (node.HasChildNodes)
{
XmlNodeList subNodeList = node.ChildNodes;
foreach (XmlNode subNode in subNodeList)
{
if (subNode.Name == keyName)
{
if (subNode.InnerText == value)
{
rootNode.RemoveChild(subNode.ParentNode);
break;
}
}
}
}
}
}

        /// <summary>
        /// 删除第4节点
        /// </summary>
        /// <param name=”xmlFilePath”>路径</param>
        /// <param name=”RootPath”>根节点</param>
        /// <param name=”FirstNodeName”>第三节点名</param>
        /// <param
name=”FirstNodeAttributes”>首节点属性名</param>
        /// <param
name=”FirstNodeAttributesValue”>第二节点属性值</param>
        public void DeleteXmlFirstnode(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

}

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             //node.RemoveAll();
                             rootNode.RemoveChild(node);
                         }
                     }
                 }
                 //保存对xml文件所做的退换
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

public List<List<string>> GetAllKeyValues(string
rootNodeName)
{
List<List<string>> listRet = new
List<List<string>>();
XmlNode rootNode = doc.SelectSingleNode(rootNodeName);
if (rootNode.HasChildNodes)
{
XmlNodeList nodeList = rootNode.ChildNodes;

        /// <summary>
         /// 删除子节点
        /// </summary>
        /// <param name=”xmlFilePath”>路径</param>
        /// <param
name=”FirstElementattributesName”>第二节点属性名</param>
        /// <param
name=”Firstattributes”>第3节点属性值</param>
        /// <param
name=”secondnodeName”>子节点名称</param>
        public void DeleteXmlsecondNode(string xmlFilePath, string
RootPath, string FirstNodeName, string FirstNodeAttributes, string
FirstNodeAttributesValue, string secondnodeName)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

foreach (XmlNode node in nodeList)
{

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if
(node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in
node.ChildNodes)
                             {
                                 if (_node.Name == secondnodeName)
                                     //_node.RemoveAll();
                                     node.RemoveChild(_node);

if (node.HasChildNodes)
{
List<string> list = new List<string>();
XmlNodeList subNodeList = node.ChildNodes;
foreach (XmlNode subNode in subNodeList)
{
list.Add(subNode.InnerText);
}
listRet.Add;
}
}
}

                             }
                         }
                     }
                 }
                 //保存对xml文件所做的修改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

return listRet;
}
}

    }
}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website