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文件介绍如何使 ...
随机推荐
- 基于python的文件处理
二.文件操作方法大全 1.os.mknod("test.txt") 创建空文件2.fp = open("test.txt",w) 直接打开一个文件,如果文件不存 ...
- iOS compare 字符串比较
NSString 比较字符串,我介绍一些常用的方法: NSString *value = @"1234567890"; 比较的方法: [value compare:(NSStrin ...
- 摇一摇js 实现
if (window.DeviceMotionEvent) { window.addEventListener('devicemotion',deviceMotionHandler, false); ...
- NYOJ题目27水池数目
--------------------------------------------- 这道题有点坑,也怪我总是有点马虎,按照正常人的思维0是表示有水池啊竟然是1表示有水池,最坑的是写反了竟然还能 ...
- 【Java EE 学习 51】【Spring学习第三天】【cglib动态代理】【AOP和动态代理】【切入点表达式】
一.cglib动态代理 1.简介 (1)CGlib是一个强大的,高性能,高质量的Code生成类库.它可以在运行期扩展Java类与实现Java接口. (2) 用CGlib生成代理类是目标类的子类. (3 ...
- 哈,我自己翻译的小书,马上就完成了,是讲用python处理大数据框架hadoop,spark的
花了一些时间, 但感觉很值得. Big Data, MapReduce, Hadoop, and Spark with Python Master Big Data Analytics and Dat ...
- jsf初学解决GlassFish Server 无法启动
由于公司需要用JSF开发项目.公司同事都不熟悉,本人C# 转JSf.开发工具 netbeans GlassFish. 遇到GlassFish 非常 纠结的问题.搞了好一段时间,, 装好GlassFis ...
- js验证输入的金钱格式
<html> <head> <title>js验证输入的金钱格式</title> <script type="text/javascri ...
- ASP.NET Core和Angular 2双剑合璧
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:两个还没有正式发布的东西一起用,是什么效果? 效果当然会很好了(我猜的),那么如何在A ...
- Linux下的tmpfs文件系统(/dev/shm)
转自:http://www.2cto.com/os/201411/354888.html 介绍 /dev/shm/是一个使用就是tmpfs文件系统的设备,其实就是一个特殊的文件系统.redhat中默认 ...