c#解析xml
贴代码
xml
<?xml version="1.0" encoding="utf-8" ?>
<CoInfo Name="Botanic" Desc="博泰康华项目" Code="BotanicApi"> <SignKey></SignKey>
<Proportion>0.05</Proportion>
<ApiAddress>http://218.94.124.235:8086/</ApiAddress>
<Interfaces>
<InterfaceOption Name="Categories" ActionUrl="http://218.94.124.235:8086/Api/ProductsApi/GetCategories" IsHtml="False">商品类别api</InterfaceOption>
<InterfaceOption Name="Products" ActionUrl="http://218.94.124.235:8086/Api/ProductsApi/GetProducts" IsHtml="False">商品信息api</InterfaceOption>
</Interfaces>
<LianLianPay>
<PartnerConfig Name="YT_PUB_KEY" Remark="RSA银通公钥">MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSS/DiwdCf/aZsxxcacDnooGph3d2JOj5GXWi+q3gznZauZjkNP8SKl3J2liP0O6rU/Y/+IUe+GTMhMOFJuZm1htAtKiu5ekW0GlBMWxf4FPkYlQkPE0FtaoMP3gYfh+OwI+fIRrpW3ySn3mScnc6Z700nU/VYrRkfcSCbSnRwIDAQAB</PartnerConfig>
</LianLianPay>
<Udcredit>
<UdcreditConfig Name="VerificationIdCard_SERVICE_URL" Remark="有盾服务调用地址,身份证" ActionUrl="https://api.udcredit.com/api/credit/v1/get_nauth" Value="">有盾服务调用地址身份证</UdcreditConfig>
</Udcredit> </CoInfo>
实体类
public partial class BotanicApiConfig
{
public BotanicApiConfig()
{
InterfaceOptions = new Dictionary<string, InterfaceOption>();
PartnerConfig = new Dictionary<string, PartnerConfig>();
UdcreditConfig=new Dictionary<string, UdcreditConfig>();
} public string SignKey { get; set; } public string Proportion { get; set; } public string ApiAddress { get; set; }
public Dictionary<string, InterfaceOption> InterfaceOptions { get; set; } public Dictionary<string, PartnerConfig> PartnerConfig { get; set; } public Dictionary<string, UdcreditConfig> UdcreditConfig { get; set; }
} public class InterfaceOption
{
public string Name { get; set; } public string ActionUrl { get; set; } public bool IsHtml { get; set; } } public class PartnerConfig
{
public string Name { get; set; } public string Value { get; set; }
} public class UdcreditConfig
{
public string Name { get; set; } public string ActionUrl { get; set; } public string Remark { get; set; } public string Value { get; set; }
}
解析
public class BotanicApiEngine
{
#region private static Utilities.BotanicApiConfig _botanicApiConfig;
private const string XmlPath = "~/App_Data/BotanicApi.xml"; #endregion
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
public static Utilities.BotanicApiConfig Instance()
{
if (_botanicApiConfig == null)
{
Initialize(HostingEnvironment.MapPath(XmlPath));
}
return _botanicApiConfig;
}
/// <summary>
/// 解析xml
/// </summary>
/// <param name="configFile"></param>
public static void Initialize(string configFile)
{
var document = new XmlDocument();
document.Load(configFile);
var section = document.SelectSingleNode("CoInfo");
var botanicCofig = new Utilities.BotanicApiConfig();
//获取signkey
var dynamicDiscoveryNode = section.SelectSingleNode("SignKey");
if (dynamicDiscoveryNode != null)
botanicCofig.SignKey = dynamicDiscoveryNode.InnerText; //获取Proportion
dynamicDiscoveryNode = section.SelectSingleNode("Proportion");
if (dynamicDiscoveryNode != null)
botanicCofig.Proportion = dynamicDiscoveryNode.InnerText; //获取ApiAddress
dynamicDiscoveryNode = section.SelectSingleNode("ApiAddress");
if (dynamicDiscoveryNode != null)
botanicCofig.ApiAddress = dynamicDiscoveryNode.InnerText; //获取接口信息
dynamicDiscoveryNode = section.SelectSingleNode("Interfaces");
if (dynamicDiscoveryNode != null)
{
foreach (XmlNode node in dynamicDiscoveryNode.ChildNodes)
{
Utilities.InterfaceOption interfaceOption = new Utilities.InterfaceOption();
var attr = node.Attributes["Name"];
if (attr != null)
interfaceOption.Name = attr.Value;
attr = node.Attributes["ActionUrl"];
if (attr != null)
interfaceOption.ActionUrl = attr.Value;
attr = node.Attributes["IsHtml"];
if (attr != null)
{
interfaceOption.IsHtml = Convert.ToBoolean(attr.Value);
}
if (!botanicCofig.InterfaceOptions.ContainsKey(interfaceOption.Name))
{
botanicCofig.InterfaceOptions.Add(interfaceOption.Name, interfaceOption);
}
}
} dynamicDiscoveryNode = section.SelectSingleNode("LianLianPay");
if (dynamicDiscoveryNode != null)
{
foreach (XmlNode node in dynamicDiscoveryNode.ChildNodes)
{
PartnerConfig partnerConfig = new PartnerConfig();
var attr = node.Attributes["Name"];
if (attr != null)
partnerConfig.Name = attr.Value;
partnerConfig.Value = node.InnerText;
if (!botanicCofig.PartnerConfig.ContainsKey(partnerConfig.Name))
{
botanicCofig.PartnerConfig.Add(partnerConfig.Name, partnerConfig);
}
}
} dynamicDiscoveryNode = section.SelectSingleNode("Udcredit");
if (dynamicDiscoveryNode != null)
{
foreach (XmlNode node in dynamicDiscoveryNode.ChildNodes)
{
var udcreditConfig = new Utilities.UdcreditConfig();
var attr = node.Attributes["Name"];
if (attr != null)
udcreditConfig.Name = attr.Value;
attr = node.Attributes["Remark"];
if (attr != null)
udcreditConfig.Remark = attr.Value;
attr = node.Attributes["ActionUrl"];
if (attr != null)
{
udcreditConfig.ActionUrl = attr.Value;
}
attr = node.Attributes["Value"];
if (attr != null)
{
udcreditConfig.Value = attr.Value;
}
if (!botanicCofig.UdcreditConfig.ContainsKey(udcreditConfig.Name))
{
botanicCofig.UdcreditConfig.Add(udcreditConfig.Name, udcreditConfig);
}
}
}
_botanicApiConfig = botanicCofig;
} }
使用
public static readonly string SignKey = BotanicApiEngine.Instance().SignKey;
public static readonly string AddressesApi = BotanicApiEngine.Instance().InterfaceOptions["Addresses"].ActionUrl;
c#解析xml的更多相关文章
- Android 解析XML文件和生成XML文件
解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...
- Android之解析XML
1.XML:可扩展标记语言. 可扩展标记语言是一种很像超文本标记语言的标记语言. 它的设计宗旨是传输数据,而不是显示数据. 它的标记没有被预定义.需要自行定义标签. 它被设计为具有自我描述性. 是W3 ...
- Android之Pull解析XML
一.Pull解析方法介绍 除了可以使用SAX和DOM解析XML文件,也可以使用Android内置的Pull解析器解析XML文件.Pull解析器的运行方式与SAX解析器相似.它也是事件触发的.Pull解 ...
- Android之DOM解析XML
一.DOM解析方法介绍 DOM是基于树形结构的节点或信息片段的集合,允许开发人员使用DOM API遍历XML树,检索所需数据.分析该结构通常需要加载整个文档和构造树形结构,然后才可以检索和更新节点信息 ...
- Android之SAX解析XML
一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...
- Android 使用pull,sax解析xml
pull解析xml文件 1.获得XmlpullParser类的引用 这里有两种方法 //解析器工厂 XmlPullParserFactory factory=XmlPullParserFactory. ...
- 用 ElementTree 在 Python 中解析 XML
用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...
- java解析xml的三种方法
java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...
- WP8解析XML格式文件
DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式,如果要返回XML格式的话,需要在加上format=xml. 这里举一个简单的解析XML格式的例子(更 ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
随机推荐
- 利用js刷新页面方法
1,reload 方法,该方法强迫浏览器刷新当前页面. location.reload(force) 如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-S ...
- 欢迎加入threejs
Threejs is the coolest graphics rendering engine I have ever seen, so what is threejs, Now, we have ...
- W7无法更新
从提示中可以推断可能服务中没有启动更新服务,当即开始>>>运行>>>services.msc 打开服务管理,找到Windows Update服务,启动它.重新更新服 ...
- Java 自动装箱、拆箱机制及部分源码分析
Integer i = 10; //装箱,反编译后发现调用Integer.valueOf(int i) int t = i; //拆箱,反编译后发现调用i.intValue() public clas ...
- plist文件的读写
参考资料 http://blog.csdn.net/totogo2010/article/details/7634185
- Odoo中最小库存规则和按订单生成规则的区别
---恢复内容开始--- 最小库存规则(Minimum stock rule)用来保证你的库存产品数量总是不会低于设定的最小库存数量.用来保证产品生产和回到客户的需求.当库存产品低于这个最小库存数量时 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)
框架整合测试程序开发 (1).在mysql数据库中创建t_user表,sql语句如下 CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INC ...
- 面向切面编程AOP
本文的主要内容(AOP): 1.AOP面向切面编程的相关概念(思想.原理.相关术语) 2.AOP编程底层实现机制(动态代理机制:JDK代理.Cglib代理) 3.Spring的传统AOP编程的案例(计 ...
- hive2.1.0安装
下载hive(http://mirrors.cnnic.cn/apache/hive/) 或者 http://archive.apache.org/dist/hive/(hive历史版本) 在本地进行 ...
- CentOS7— Redis安装(转和延续)
Part I. Redis安装(转载部分) 一.安装 wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.t ...