读取xml文件中节点
1、【在解决方案下新建Xml文件 并设置demo值】


2、【设置Xml文件属性为 始终复制】

3、【代码】
/// <summary>
/// Xml文件名
/// </summary>
private const string _xmlFilePath = "XMLFileDemo.xml";
/// <summary>
/// 获取Xml文件中节点的值
/// </summary>
/// <param name="value"></param>
/// <param name="xmlPath">节点名称</param>
/// <returns></returns>
public static string GetPropertyValue(ref string value, string xmlPath)
{
try
{
XmlDocument xml = new XmlDocument();
xml.Load(_xmlFilePath);
XmlNodeList xmlNode = xml.SelectNodes(xmlPath);
if (xmlNode.Count==)
{
throw new ApplicationException(string.Format("{0}文件中不存在节点{1}", _xmlFilePath, xmlPath));
}
value = xmlNode[].InnerXml;
return value;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
/// <summary>
/// 更改Xml文件中某一节点的值
/// </summary>
/// <param name="value"></param>
/// <param name="xmlPath"></param>
public static void SetPropertyValue(string value, string xmlPath)
{
try
{
XmlDocument xml = new XmlDocument();
xml.Load(_xmlFilePath);
XmlNodeList xmlNode = xml.SelectNodes(xmlPath);
if (xmlNode.Count == )
{
throw new ApplicationException(string.Format("{0}文件中不存在节点{1}", _xmlFilePath, xmlPath));
}
xmlNode[].InnerXml = value;
xml.Save(_xmlFilePath);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
4、【调用Demo】
public class Program
{
private string _myProperty;
public string MyProperty {
get
{
return SingleDemo.GetPropertyValue(ref _myProperty, "Settings/Name");
}
set {
SingleDemo.SetPropertyValue(value, "Settings/Name");
}
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.MyProperty);
Console.WriteLine("请输入变化后的值:");
string s=Console.ReadLine();
p.MyProperty = s;
Console.WriteLine("变化后Property的值:"+p.MyProperty);
Console.ReadLine();
}
}

读取xml文件中节点的更多相关文章
- .net中读取xml文件中节点的所有属性信息
功能描述: 将数据以xml的格式记录成配置文件,需要获取配置文件中的数据时,则获取对应的配置文件,读取配置文件里对应节点的所有属性. 逻辑实现: 1.将数据配置好在xml文件中. 2.获取xml文件中 ...
- C#读取xml文件指定节点下的值
#region 读取xml文件指定节点下的值 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(result); XmlNode root ...
- 读取xml文件中的配置参数实例_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 paras.xml文件 <?xml version="1.0" encoding=" ...
- C#中读取xml文件指定节点
目录(?)[-] XmlDocumentSelectSingleNode方法的使用 XmlDocumentSelectNodes方法的使用 通过节点属性查找指定节点 参考:Select XML N ...
- python读取xml文件中的坐标点
用labelImg工具制作好xml文件后,需要读取其中img路径和坐标点,生成一个label.txt <annotation> <folder>big</folder&g ...
- vue项目中使用bpmn-流程图xml文件中节点属性转json结构
内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...
- python中用ElementTree.iterparse()读取xml文件中的多层节点
我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...
- PHP读取XML文件数据获取节点值
最近在接入渠道的时候遇到接口返回是xml数据.现在接口数据返回json数据格式比较常见. 如何获取xml里面真正数据? 对象结果集合单个值的强制转换处理.(直接代码说明) demo示例: 创建xml ...
- 读取XML文件的节点内的内容
<?xml version="1.0" encoding="utf-8" ?> <data> <tag3>15</ta ...
随机推荐
- (转)盒子概念和DiV布局
CSS盒子和DIV布局 (2013-11-24 16:17:29) 转载▼ 一.认识div层 1.<DIV>标记是一个区块容器标记,在标记之间可以放置其他一些HTML元素,例如p,h1,t ...
- 云游戏学习与实践(二)——安装GamingAnywhere
安装GamingAnywhere 一.GamingAnywhere项目 GamingAnywhere是一个开源的实现云游戏的引擎,并且高效.跨平台.易扩展.可调配. GitHub地址:https:// ...
- 【samba】samba 用户权限配置(转)
首先要保证你的samba安装并配置好,关于安装和配置samba请参考此文章 http://blog.csdn.net/linglongwunv/archive/2010/01/19/5212875.a ...
- 百度地图JavaScript API使用
最近在完成优达学城前端开发(入门)课程的P4项目中,要求调用google地图进行交互,项目已提供部分js代码和html代码.但在申请google地图API密钥时由于网络等原因,打不开或者连接超时,所以 ...
- Centos6.5中Nginx部署基于IP的虚拟…
Centos6.5 中Nginx 部署基于IP 的虚拟主机 王尚2014.11.18 一.介绍虚拟主机 虚拟主机是使用特殊的软硬件技术,把一台真实的物理电脑主机 分割成多个逻辑存储单元,每个单元都没有 ...
- [js高手之路] es6系列教程 - 函数的默认参数详解
在ES6之前,我们一般用短路表达式处理默认参数 function show( a, b ){ var a = a || 10; var b = b || 20; console.log( a, b ) ...
- 【前端,干货】react and redux教程学习实践(二)。
前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...
- Fluent Validation with Web Api 2
using FluentValidation;using FluentValidation.Attributes;using System;using System.Collections.Gener ...
- 【python密码学编程】5.反转加密法
#Reverse Cipher message = 'there can keep a secret,if two of them are dead.' translated = '' i = len ...
- 【HOSTS相关】什么时候使用127.0.0.1
什么时候使用127.0.0.1 在测试环境,如果想知道上线后服务发生异常中断的情况下界面会如何展示,这个时候有2种方式: 第1种是:由开发人员协助,比如后台开发人员在服务器上使这个服务停止,或者由前端 ...