常用的方法,读取XML节点并赋值给List集合
一、前言
很多时候也可以直接在XML文件中配置好节点,在程序需要用到的时候,修改XML文件并不需要重新编译,这里是在极光推送中拿出来的一部分代码。代码简单,大家直接看例子吧。
二、实现过程
1.新创建一个AppKey.xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<JPushKeyList>
<PushKey>
<AppKey>1111</AppKey>
<MasterSecret>2222</MasterSecret>
</PushKey>
<PushKey>
<AppKey>333</AppKey>
<MasterSecret>444</MasterSecret>
</PushKey>
<PushKey>
<AppKey>555</AppKey>
<MasterSecret>666</MasterSecret>
</PushKey>
</JPushKeyList>
<EngJPushKeyList>
</EngJPushKeyList>
</root>
2.获取PushKey的List集合方法
private static List<PushServer.JPushKey> PushKeyListGet()
{
List<PushServer.JPushKey> jPushKeyList = new List<PushServer.JPushKey>();
//string xmlFilePath = HttpRuntime.AppDomainAppPath + "AppKey.xml";
string xmlFilePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "AppKey.xml");
XmlNodeList nodeList;
nodeList = Utility.XMLHelper.GetXmlNodeListByXpath(xmlFilePath, "/root/JPushKeyList/PushKey");
foreach (XmlNode n in nodeList)
{
string strAppKey = n.SelectSingleNode("AppKey").InnerXml;
string strMasterSecret = n.SelectSingleNode("MasterSecret").InnerXml;
PushServer.JPushKey key = new PushServer.JPushKey(strAppKey, strMasterSecret);
jPushKeyList.Add(key);
}
return jPushKeyList;
}
3. 选择匹配XPath表达式的节点列表XmlNodeList
/// <summary>
/// 选择匹配XPath表达式的节点列表XmlNodeList.
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>
/// <returns>返回XmlNodeList</returns>
public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);
return xmlNodeList;
}
catch (Exception ex)
{
return null;
//throw ex; //这里可以定义你自己的异常处理
}
}
常用的方法,读取XML节点并赋值给List集合的更多相关文章
- C#读取xml节点数据方法小结
本文实例总结了C#读取xml节点数据的方法.分享给大家供大家参考.具体如下: 第一种: 使用XPath XML的路径我配置在web.config 的appSettings节点下 <appSett ...
- flash读取XML节点内容以及节点属性
原文地址:http://hi.baidu.com/yqzdm/item/f95fd9d24679d916d90e44c9 一.xml的写法: 这里的xml只是在有限范围内的了解,限于写一些简单的用于f ...
- Dom4j使用Xpath语法读取xml节点
我们可以使用Xpath的语法来轻易的读取xml的某个节点[类似于jQuery的选择器]: 使用Xpath语法需要添加新的jaxen-1.1-beta-7.rar 这个jar包 dom4j完整jar包我 ...
- 用 query 方法 获得xml 节点的值
DECLARE @result xml SET @result='<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelo ...
- C# 读取XML节点属性值
xml文件格式如下: <?xml version="1.0" encoding="UTF-8" ?> <Product type=" ...
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
转自:http://blog.csdn.net/zhao19861029/article/details/8473245 首先实现生成对应的JAVAbean的XML文件方法 /** * DMO4J写入 ...
- dom方法读取xml(不常用)
Book.java package com.xml.demo; public class Book { private int id; private String name; private Flo ...
- 使用C#读取XML节点,修改XML节点
例子: <add key="IsEmptyDGAddRootText" value="" /> <!--是否可以修改归档状态档案 false: ...
- C# 读取xml节点类容
这是一个测试节点类容的获取 这是控制台代码部分 注意的应用文件 :using.system.Xml using System; using System.Collections.Generic; us ...
随机推荐
- poj1915 BFS
D - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:30000KB 64bi ...
- set用法总结
set集合用于存放不重复的元素 template <class Key, class Compare = less<Key>, class Alloc = alloc> cla ...
- Makefile的几个赋值运算符(转:笔记)
http://www.cnblogs.com/pengdonglin137/p/3801060.html 在Makefile中我们经常看到 = := ?= +=这几个赋值运算符,那么他们有什么区别呢? ...
- Windows10 Ubuntu子系统折腾
UPDATE:(参考文章) 快速解决方案 使用cmder,设置startup参数为: %windir%\system32\bash.exe ~ 这样打开cmder就是默认进入bash了. ------ ...
- Bootstrap 模态对话框 remote指定内容加载
第一个页面: .....其他内容..... <div class="modal" id="ID_ReformDetail"> <div cla ...
- s3c2416裸跑环境配置
最近刚刚开始学习ARM-linux,上周买了块tq2416的板子,给的Linux资料太复杂太深奥不愿看,等不及想要把2416跑起来.于是到处找相关裸跑资料,可是用2416的人实在少,网上的资料更少,裸 ...
- mysql 命令行 自动补全
配置文件中 默认关闭自动补全: [mysql] #no-auto-rehash # faster start of mysql but no tab completition 改为: [mysql] ...
- [科普]MinGW vs MinGW-W64及其它
转载:http://tieba.baidu.com/p/3186234212?pid=54372018139&cid=#54372018139 这里也转一下吧. 部分参照备忘录原文: bitb ...
- VIm变成sublime (转)
sublime在ubuntu下始终支持不是很好, 特别是对中文输入的支持,还有一些插件在ubuntu下也不能用. 在ubuntu下还是用vim吧. 我们一起把vim变成sublime. 只需要三步 ...
- 谷歌Cartographer学习(2)-原理阐述与源码解析
最近终于写完了毕业论文.想仔细研究下Cartographer.无奈自己学识有限,先看下网上大牛的解析,作一个汇总. 一.泡泡机器人原创专栏-cartographer理论及实践浅析 http://mp. ...