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文件介绍如何使 ...
随机推荐
- jQuery UI Datepicker
http://www.runoob.com/try/try.php?filename=jqueryui-example-datepicker-dropdown-month-year <!doct ...
- objccn-图片格式
图像格式存储:位图和矢量图像.位图把值存在阵列中,矢量格式存储的是绘图图像的指令.还有混合格式PostScript能够排布字母甚至位图,使其成为了一个非常灵活的方式.衍生格式pdf. xcdoe6已经 ...
- iOS compare 字符串比较
NSString 比较字符串,我介绍一些常用的方法: NSString *value = @"1234567890"; 比较的方法: [value compare:(NSStrin ...
- gulp基本介绍
一.gulp是什么 gulp就是用来机械化的完成重复性质(如less->css:js.css压缩:js混淆)的工作:gulp的机制就是将重复工作抽象成一个个的任务. 二.gulp使用 a.首先确 ...
- iOS开发UI篇—CAlayer(创建图层)
iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...
- 踩个猴尾不容易啊 Canvas画个猴子
踩个猴尾不容易啊 Canvas画个猴子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- Hotaru's problem(hdu 5371)
题意:给出一个数字串,询问最长的子串,满足以下要求:将子串平均分为三部分,一三部分相等,一二部分对衬. /* 在manachar的基础上,枚举回文串的中心,再找第三部分. */ #include< ...
- 浅析session&cookie
session&cookie没有出现的黑暗时代 大家都知道,HTTP协议是一种无状态的协议,本次请求下一次请求没有任何的关联,所有没有办法直接用http协议来记住用户的信息,试想一向,每一次点 ...
- iOS开发的小技巧
转自简书:http://www.jianshu.com/p/50b63a221f09 http://www.jianshu.com/p/08f194e9904c 原作者:叶孤城___ self.ta ...
- Android 应用程序升级到 5.0 需要注意的问题
Android 5.0,代号 Lollipop,源码终于在2014年12月3日放出,国内一大批厂商跟进.最大的改变是默认使用 ART(Android Runtime) ,替换了之前的 Dalvik 虚 ...