C#获取和更改xml文件(配置文件)
来源:互联网 发布:淘宝的返利机器人 编辑:程序博客网 时间:2024/06/06 16:28
/// <summary>
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateGSTCDValue(/*string strFileName, */string nodeName, string gstcdValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
xe.SetAttribute("GSTCDMAX", gstcdValue);
isSucceed = true;
break;
}
}
xmlDoc.Save(str);
}
catch (Exception ex)
{
throw ex;
}
return isSucceed;
}
public static bool UpdateSSTCDValue(/*string strFileName, */string nodeName, string sstcdValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
xe.SetAttribute("SSTCDMAX", sstcdValue);
isSucceed = true;
break;
}
}
xmlDoc.Save(str);
}
catch (Exception ex)
{
throw ex;
}
return isSucceed;
}
/// <summary>
/// 获取指定节点下面对应属性的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <param name="attribute">节点对应的属性名称</param>
/// <returns></returns>
public static string GetNodeValue(string nodeName)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
//value = xe.InnerText.Trim();
value = (xe).Attributes["STTP"].Value;
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
/// 修改指定结点值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public static bool UpdateGSTCDValue(/*string strFileName, */string nodeName, string gstcdValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
xe.SetAttribute("GSTCDMAX", gstcdValue);
isSucceed = true;
break;
}
}
xmlDoc.Save(str);
}
catch (Exception ex)
{
throw ex;
}
return isSucceed;
}
public static bool UpdateSSTCDValue(/*string strFileName, */string nodeName, string sstcdValue)
{
bool isSucceed = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
xe.SetAttribute("SSTCDMAX", sstcdValue);
isSucceed = true;
break;
}
}
xmlDoc.Save(str);
}
catch (Exception ex)
{
throw ex;
}
return isSucceed;
}
/// <summary>
/// 获取指定节点下面对应属性的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <param name="attribute">节点对应的属性名称</param>
/// <returns></returns>
public static string GetNodeValue(string nodeName)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str = str.Substring(0, str.Length - 10) + "App.config";
xmlDoc.Load(str);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("configuration").ChildNodes;//获取configuration节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
//判断如果是注释则过滤掉
if (xn is XmlComment)
{
continue;
}
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
//value = xe.InnerText.Trim();
value = (xe).Attributes["STTP"].Value;
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
0 0
- C#获取和更改xml文件(配置文件)
- C#读写配置文件(XML文件)
- C#读写配置文件(XML文件)
- C#获取远程xml文件
- C#获取远程xml文件
- C# 读写XML文件,用于配置文件
- C#中读取和更改APP.config配置文件
- 更改用户配置文件Config.xml
- XML文件更改插入
- PHP获取XML配置文件
- C#读取XML配置文件
- C#操作XML配置文件
- C# 读写XML配置文件
- C#读取XML配置文件
- C#操作XML配置文件
- c#读取xml文件配置文件Winform及WebForm-Demo详解
- C# 文件更改扩展名
- Struts2更改配置文件struts.xml默认路径
- 数据库索引
- Java中Volatile关键字详解
- 洛谷 P2221 [HAOI2012] 高速公路
- 远程桌面无法复制粘贴
- 字符编码问题
- C#获取和更改xml文件(配置文件)
- 使用DrawerLayout和Toolbar实现的侧滑菜单修改默认图标
- git记住用户名和密码
- python自然语言处理-WordNet
- 客户端合成gif
- 如何将mongodb-c-driver交叉编译到ARM平台
- C++函数返回类型学习笔记
- Unity3D-5.5版本的IAP-IOS使用方式(In-App-Purchase)
- java static关键字