在应用程序开发中,XML配置文件是大多程序不可缺少的东西。它可以直接保存下我们程序的一些重要数据和配置,而不需要数据库来储存,给我们带来了非常大的便利。C#对于XML类型的操作比起Flex中对XML的操作就难多了,下面分享下我以前封装的一个XML操作类。
1.获取XML数据,直接返回DataTable
/// <summary>
/// 获取XML数据
/// </summary>
/// <param name="xmlPath">XML文件路径</param>
/// <param name="singleNode">XMLNode</param>
/// <param name="attributeName">属性名</param>
/// <param name="nodeName">Nodes</param>
/// <returns>Table表</returns>
public static DataTable GetXMLData(string xmlPath,string singleNode,string attributeName,string[] nodeName)
{
XmlDocument XmlDocument = new XmlDocument();
XmlDocument.Load(xmlPath);
XmlNodeList nodeList = XmlDocument.SelectSingleNode(singleNode).ChildNodes;//获取Employees节点的所有子节点
DataTable mDataTable = new DataTable();
mDataTable.Rows.Clear();
mDataTable.Columns.Add(attributeName);
foreach (string node in nodeName)
{
mDataTable.Columns.Add(node);
}
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
int itemIndex = 1;
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
string appName = xe.GetAttribute(attributeName);
DataRow mDataRow = mDataTable.NewRow();
mDataRow[0] = appName;
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
mDataRow[itemIndex] = xe2.InnerText;
itemIndex ;
}
mDataTable.Rows.Add(mDataRow);
}
return mDataTable;
}2.添加XML数据
/// <summary>
/// 添加数据
/// </summary>
/// <param name="xmlPath">XML文件路径</param>
/// <param name="singleNode">XMLNode</param>
/// <param name="elementName">元素</param>
/// <param name="appAttributeName">属性名</param>
/// <param name="appAttributeValue">属性值</param>
/// <param name="appNodes">Nodes</param>
/// <param name="nodesValue">值</param>
public static void AddXMLData(string xmlPath, string singleNode, string elementName, string appAttributeName, string appAttributeValue, string[] appNodes, string[] nodesValue)
{
if (xmlPath != "" && singleNode != "" && elementName != "" && appAttributeName != "" && appAttributeValue != "")
{
if (IsHasXMLNode(xmlPath, singleNode, appAttributeName, appAttributeValue, appNodes) == true)
{
DeleteXMLData(xmlPath, singleNode, appAttributeName, appAttributeValue);
}
XmlDocument mXmlDocumentApp = new XmlDocument();
mXmlDocumentApp.Load(xmlPath);
XmlNode mXmlNode = mXmlDocumentApp.SelectSingleNode(singleNode);
XmlElement mXmlElementAppName = mXmlDocumentApp.CreateElement(elementName);
mXmlElementAppName.SetAttribute(appAttributeName, appAttributeValue);
for (int i = 0; i < appNodes.Length; i )
{
//添加ItemPath节点
XmlElement mXmlElementItemPath = mXmlDocumentApp.CreateElement(appNodes[i]);
mXmlElementItemPath.InnerText = nodesValue[i];
mXmlElementAppName.AppendChild(mXmlElementItemPath);
}
mXmlNode.AppendChild(mXmlElementAppName);
mXmlDocumentApp.Save(xmlPath);
}
}3.修改属性
/// <summary>
/// 修改属性
/// </summary>
/// <param name="xmlPath"></param>
/// <param name="singgleNode"></param>
/// <param name="attributeName"></param>
/// <param name="attributeValue"></param>
/// <param name="attributeNewValue"></param>
public static void UpdateXMLData(string xmlPath, string singgleNode, string attributeName, string attributeValue, string attributeNewValue)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(singgleNode).ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute(attributeName) == attributeValue)
{
xe.SetAttribute(attributeName, attributeNewValue);
}
}
xmlDoc.Save(xmlPath);
}4.删除节点
/// <summary>
/// 删除节点
/// </summary>
/// <param name="xmlPath"></param>
/// <param name="singgleNode"></param>
/// <param name="attributeName"></param>
/// <param name="attributeValue"></param>
public static void DeleteXMLData(string xmlPath, string singgleNode, string attributeName, string attributeValue)
{
XmlDocument mXmlDocument = new XmlDocument();
mXmlDocument.Load(xmlPath);
XmlNode mXmlNode = mXmlDocument.SelectSingleNode(singgleNode);
XmlNodeList mXmlNodeList = mXmlDocument.SelectSingleNode(singgleNode).ChildNodes;
for (int i = 0; i < mXmlNodeList.Count; i )
{
XmlElement xe = (XmlElement)mXmlNodeList.Item(i);
if (xe.GetAttribute(attributeName) == attributeValue)
{
mXmlNode.RemoveChild(xe);
if (i < mXmlNodeList.Count) i = i - 1;
}
}
mXmlDocument.Save(xmlPath);
}5.创建XML文件
public static void CreateXML(string xmlPath, string singgleName, string[] nodes, string[] nodesValues)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
//加入根元素
XmlElement xmlElem = xmlDoc.CreateElement("", singgleName, "");
xmlDoc.AppendChild(xmlElem);
//FileName
for (int i = 0; i < nodes.Length; i )
{
XmlElement xmlElemFileName = xmlDoc.CreateElement(nodes[i]);
XmlText xmlTextFileName = xmlDoc.CreateTextNode(nodesValues[i]);
xmlElemFileName.AppendChild(xmlTextFileName);
xmlDoc.ChildNodes.Item(1).AppendChild(xmlElemFileName);
}
xmlDoc.Save(xmlPath);
}彭亚欧个人博客原创文章,转载请注明出处
文章关键词:C# XML操作
文章固定链接:https://www.pengyaou.com/homeart/NTg=.html