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文件介绍如何使 ...
随机推荐
- 项目vue2.0仿外卖APP(一)
最近用vue.js做一个仿饿了么外卖APP的项目,现在也把流程啊什么的暂时先整理一下在这个博客上面. 当然,这个过程会有点长,不过确实能学到很多东西. 话不多说,马上开始吧. 1.项目介绍 选用当前最 ...
- python function parameter
Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...
- Python日志logging
logging 用于便捷记录日志且线程安全的模块 1.单文件日志 import logging logging.basicConfig(filename='log.log', format='%(as ...
- hmm
http://www.cnblogs.com/mindpuzzle/p/3653043.html http://en.wikipedia.org/wiki/Viterbi_algorithm http ...
- Angularjs 脏值检测
文章转自:http://www.ituring.com.cn/article/39865 构建自己的AngularJS,第一部分:Scope和Digest 原文链接:http://teropa.inf ...
- LVS集群之NAT模式实现
LVS集群之NAT模式实现 一.集群的种类 集群系统主要分为 1.HA:高可用集群,又叫双机热备. (a)原理 2台机器A,B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至 ...
- showModalDialog打开页面有缓存,不走action
当你设置的弹出网页固定时,ie很可能到临时文件区,下载上次产生的该页面,而没有重新加载, 对于动态加载的页面来说,这样往往产生误会,如没有及时更新数据,也就更不利于开发者测试.所以,你可以采用如 ...
- SSO 单点登录实现
.NET基于Redis缓存实现单点登录SSO的解决方案 http://www.cnblogs.com/yinrq/p/5276628.html 共享cookie的方案 http://www.codep ...
- iOS //清除本地缓存
//清除本地缓存 -(void)clearCache{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT ...
- 2.goldengate日常维护命令(转载)
goldengate日常维护命令 发表于 2013 年 7 月 4 日 由 Asysdba 1.查看进程状态 GGSCI (PONY) 2> info all 2.查看进程详细状态,有助于排错 ...