C#中读写Xml配置文件常用方法工具类
<h1>场景</h1><p>偶然必要使用配置文件生存一些配置的属性,使其在下次打开时设置仍然生效。</p>
<p>这里以对xml配置文件的读写为例。</p>
<p>1.读取XML配置文。</p>
<p>2.写入XML配置文件。</p>
<p>3.匹配 XPath 表达式的第一个 XmlNode。</p>
<p>4.获取节点text。</p>
<p>5.根据xPath获取节点个数。</p>
<p>6.根据xPath获取节点列表。</p>
<p>以上功能只是根据本身实际业务封装的方法,其他方法可以根据本身的实际业务去封装。</p>
<h1>实现</h1>
<p>在项目目次下新建xml配置文件。</p>
<p><div align="center"></div></p>
<p> </p>
<p> </p>
<p><div align="center"><img/></div></p>
<p>代码:</p>
class ReadXMLConfigHelper
{
#region 读取XML配置文件
/// <summary>
/// 读取XML配置文件失败返回null
/// </summary>
/// <param name="filePath">xml文件路径,相对于bin下debug目次</param>
/// <returns>xml文档对象</returns>
public static XmlDocument readXml(string filePath)
{
//获取可执行文件的路径-即bin目次下的debug或者release目次
string context = System.Windows.Forms.Application.StartupPath;
//拼接全路径
string path = String.Concat(context, filePath);
XmlDocument xml = new XmlDocument();
//打开一个xml
try
{
xml.Load(path);
return xml;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
#endregion
#region 写入XML配置文件
/// <summary>
///写入XML配置文件 乐成返回True 失败返回false
/// </summary>
/// <param name="xml">xml对象</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static BooleanwriteXml(XmlDocument xml , string filePath)
{
//获取可执行文件的路径-即bin目次下的debug或者release目次
string context = System.Windows.Forms.Application.StartupPath;
//拼接全路径
string path = String.Concat(context, filePath);
try
{
xml.Save(path);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
#endregion
#region 匹配 XPath 表达式的第一个 XmlNode
/// <summary>
/// 匹配 XPath 表达式的第一个 XmlNode
/// </summary>
/// <param name="xml">xml文档对象</param>
/// <param name="xPath">xPath-路径匹配表达式</param>
/// <returns>xml节点对象失败返回Null</returns>
public static XmlNode getXmlNode(XmlDocument xml,string xPath)
{
//选择匹配 XPath 表达式的第一个 XmlNode
XmlNode xmlNode = xml.SelectSingleNode(xPath);
//读取节点数据
if (xmlNode != null)
{
return xmlNode;
}
else
{
return null;
}
}
#endregion
#region 获取节点text
/// <summary>
/// 获取节点text
/// </summary>
/// <param name="xml获取节点textNode">节点对象</param>
/// <returns>返回null则失败,返回""则代表节点内容为空,乐成返回节点text</returns>
public static string getNodeText(XmlNode xmlNode)
{
//读取节点数据
if (xmlNode != null)
{
string nodeText = xmlNode.InnerText;
if (nodeText != null)
{
return nodeText;
}
else
{
return "";
}
}
else
{
return null;
}
}
#endregion
#region 根据xPath获取节点个数
/// <summary>
/// 根据xPath获取节点个数
/// </summary>
/// <param name="xml">xml文档对象</param>
/// <param name="xPath">xPath表达式</param>
/// <returns>返回符合xPath的节点数,没有则返回0</returns>
public static int getCountByXpath(XmlDocument xml,string xPath)
{
//读取节点list
XmlNodeList nodelist = xml.SelectNodes(xPath);
if (nodelist != null)
{
return nodelist.Count;
}
else
{
return 0;
}
}
#endregion
#region 根据xPath获取节点列表
/// <summary>
///根据xPath获取节点列表
/// </summary>
/// <param name="xml">xml文档对象</param>
/// <param name="xPath">xPath表达式</param>
/// <returns>返回符合xPath的节点列表,失败返回null</returns>
public static XmlNodeList getNodeListByXpath(XmlDocument xml, string xPath)
{
//读取节点list
XmlNodeList nodelist = xml.SelectNodes(xPath);
if (nodelist != null)
{
return nodelist;
}
else
{
return null;
}
}
#endregion
}
<p> </p>
<p>调用示例:</p>
XmlDocument xml = ReadXMLConfigHelper.readXml(xmlFilePath);
XmlNodeList nodeList = ReadXMLConfigHelper.getNodeListByXpath(xml, Global.RADIO_GROUP_COUNT_XPATH);
<p> </p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/badaoliumangqizhi/archive/2019/09/16/11525950.html" target="_blank">https://www.cnblogs.com/badaoliumangqizhi/archive/2019/09/16/11525950.html</a>
页:
[1]