using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
namespace ConsoleApplication2
{
class XMLHelper
{
// <param name="xmlPath">Xml文档路径</param>
// <param name="MainNode">当前节点路径</param>
// <param name="Element">新节点</param>
// <param name="Attrib">属性名称</param>
// <param name="AttribContent">属性值</param>
// <param name="Content">新节点值</param>
//根节点下添加父节点
public static void AddParentNode(string xmlPath,string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
//创建一个新的member节点并添加到根节点下
XmlElement node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(node);
xdoc.Save(xmlPath);
}
//插一节点,带一属性
public static void XmlInsertElement(string xmlPath,string mainNode,string element,
string attrib,string attribContent,string content)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlNode objNode = xdoc.SelectSingleNode(mainNode);
XmlElement objElement = xdoc.CreateElement(element);
objElement.SetAttribute(attrib,attribContent);
objElement.InnerText = content;
objNode.AppendChild(objElement);
xdoc.Save(xmlPath);
}
//插入一个节点,不带属性
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);
}
// 向一节点添加属性
// <param name="xmlPath">xml文件路径</param>
// <param name="NodePath">节点路径</param>
// <param name="NodeAttribute1">要添加的节点属性的名称</param>
// <param name="NodeAttributeText">要添加属性的值</param>
public static void AddAttribute(string xmlPath,string nodePath1,string nodeAttribute1,string nodeAttributeText)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlAttribute nodeAttribute = xdoc.CreateAttribute(nodeAttribute1); //特性
XmlNode nodePath = xdoc.SelectSingleNode(nodePath1);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(nodeAttribute1,nodeAttributeText);
xdoc.Save(xmlPath);
}
//更新XML节点内容
// <param name="xmlPath">xml路径</param>
// <param name="Node">要更换内容的节点:节点路径 根节点/父节点/当前节点</param>
// <param name="Content">新的内容</param>
public static void XmlNodeReplace(string xmlPath,string node,string content)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
xdoc.SelectSingleNode(node).InnerText = content;
xdoc.Save(xmlPath);
}
//更新XML节点的属性值
// <param name="xmlPath">文件路径</param>
// <param name="NodePath">节点路径</param>
// <param name="NodeAttribute1">要更改的节点属性的名称</param>
// <param name="NodeAttributeText">更改的属性值</param>
public static void XmlAttributeEide(string xmlPath,string nodePath1,string nodeAttribute1,string nodeAttributeText1)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlNode nodePath = xdoc.SelectSingleNode(nodePath1);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(nodeAttribute1,nodeAttributeText1);
xdoc.Save(xmlPath);
}
// 读取XML到DataSet
public static DataSet GetXml(string xmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@xmlPath);
return ds;
}
//读取XML,返回一个节点值 读取xml文档并返回一个节点:适用于一级节点
public static string ReadXmlReturnNode(string xmlPath,string node)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlNodeList xnlist = xdoc.GetElementsByTagName(node);
return xnlist.Item(0).InnerText.ToString();
}
//查找数据,返回当前节点的下级节点,填充到一个DataSet中
public static DataSet GetXml(string xmlPath,string xmlPathNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader reader = new StringReader(xdoc.SelectSingleNode(xmlPath).OuterXml);
ds.ReadXml(reader);
return ds;
}
//读取指定节点的指定属性值
public string GetXmlNodeAttribute(string xmlPath,string strNode,string strAttribute)
{
string strReturn = "";
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
try
{
XmlNode xmlNode = xdoc.SelectSingleNode(strNode);
//获得节点的属性,并循环取出需要的属性
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for (int i = 0; i < xmlAttr.Count; i++)
{
if (xmlAttr.Item(i).Name==strAttribute)
{
strReturn = xmlAttr.Item(i).Value;
}
}
}
catch (Exception ee)
{
}
return strReturn;
}
//读取指定节点的值
public string getXmlNodeValue(string xmlPath,string strNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
string strReturn="";
try
{
//根据路径获取节点
XmlNode xmlNode = xdoc.SelectSingleNode(strNode);
strReturn = xmlNode.InnerText;
}
catch (Exception ee)
{
throw;
}
return strReturn;
}
//根据节点属性读取子节点的值
public static ArrayList GetSubElementByAttribute(string xmlPath,string fatherElement,string attributeName,string attributeValue,int arrayLength)
{
ArrayList al = new ArrayList();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlNodeList xnlist;
xnlist = xdoc.DocumentElement.SelectNodes("//"+fatherElement+"["+@attributeName+"='"+attributeValue+"']");
XmlNodeList xxlist = xnlist.Item(0).ChildNodes;
for (int i = 0; i < arrayLength&&i<xxlist.Count; i++)
{
al.Add(xxlist.Item(i).InnerText);
}
return al;
}
//删除一个节点
public static void XmlNodeDelete(string xmlPath,string node)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
string mainNode = node.Substring(0,node.LastIndexOf("/"));
xdoc.SelectSingleNode(mainNode).RemoveChild(xdoc.SelectSingleNode(node));
xdoc.Save(xmlPath);
}
//删除一个节点属性
public static void XmlNodeAttributeDelete(string xmlPath,string nodePath1,string nodeAttribute)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlNode nodepath = xdoc.SelectSingleNode(nodePath1);
XmlElement xe = (XmlElement)nodepath;
xe.RemoveAttribute(nodeAttribute);
xdoc.Save(xmlPath);
}
}
}
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string xmlPath = @"F:\前研\测试网页\WinForm\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml";
XMLHelper.AddParentNode(xmlPath,"textNode");
XMLHelper.XmlInsertElement(xmlPath, "phonebook", "textele", "shixingName", "9", "zhi");
XMLHelper.XmlInsertElement(xmlPath,"phonebook","element001","123456");
XMLHelper.AddParentNode(xmlPath, "member");
XMLHelper.AddAttribute(xmlPath,"phonebook/member","id","7");
XMLHelper.XmlInsertElement(xmlPath,"//member[@id=\"7\"]","qq","123123123");
XMLHelper.XmlNodeReplace(xmlPath,"//member[@id=6]/name","aaa");
XMLHelper.XmlNodeReplace(xmlPath, "//member[qq='5465']/name", "aaa");
XMLHelper.XmlAttributeEide(xmlPath, "//member[@id=6]/name", "cc", "bb");
XMLHelper.XmlInsertElement(xmlPath, "phonebook", "sqlConn", "server=.;uid=sa;pwd=sa");
string strConn= XMLHelper.getXmlNodeValue(xmlPath,"phonebook/member[@id=6]/qq");
Console.WriteLine(strConn);
XMLHelper.GetXmlNodeAttribute(xmlPath, "phonebook/member", "id");
XMLHelper.GetSubElementByAttribute(xmlPath,"phonebook",7);
Console.Read();
}
}
}