贴代码

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的更多相关文章

  1. Android 解析XML文件和生成XML文件

    解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...

  2. Android之解析XML

    1.XML:可扩展标记语言. 可扩展标记语言是一种很像超文本标记语言的标记语言. 它的设计宗旨是传输数据,而不是显示数据. 它的标记没有被预定义.需要自行定义标签. 它被设计为具有自我描述性. 是W3 ...

  3. Android之Pull解析XML

    一.Pull解析方法介绍 除了可以使用SAX和DOM解析XML文件,也可以使用Android内置的Pull解析器解析XML文件.Pull解析器的运行方式与SAX解析器相似.它也是事件触发的.Pull解 ...

  4. Android之DOM解析XML

    一.DOM解析方法介绍 DOM是基于树形结构的节点或信息片段的集合,允许开发人员使用DOM API遍历XML树,检索所需数据.分析该结构通常需要加载整个文档和构造树形结构,然后才可以检索和更新节点信息 ...

  5. Android之SAX解析XML

    一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...

  6. Android 使用pull,sax解析xml

    pull解析xml文件 1.获得XmlpullParser类的引用 这里有两种方法 //解析器工厂 XmlPullParserFactory factory=XmlPullParserFactory. ...

  7. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  8. java解析xml的三种方法

    java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...

  9. WP8解析XML格式文件

    DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式,如果要返回XML格式的话,需要在加上format=xml. 这里举一个简单的解析XML格式的例子(更 ...

  10. JAVA使用SAX解析XML文件

    在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...

随机推荐

  1. python基础一

    1.1 Python优点 1.简单.优雅.明确 2.强大的模块三方库 3.易移植 4.面向对象 5.可扩展(c\java\c#...) 1.2 Python缺点 1.代码不能加密 2.速度慢   1. ...

  2. JavaScript方法——call和apply

    1.相同点: a) 产生的效果或作用完全相同: b) 至少有一个参数: c) 第一个参数必须有且是一个对象(Object),因为就是这个家伙偷懒. 2.不同点: 传递参数的方式. 前提: 1.有两个对 ...

  3. 服务器监控之 Monitorix 初体验

    参考: http://www.tecmint.com/monitorix-a-lightweight-system-and-network-monitoring-tool-for-linux/ Cen ...

  4. java 深入技术四(Set)

    1)Set接口 set接口的父接口-Collection set接口的重要子类-HashSet set接口的重要子类 -TreeSet set 接口的特别子类-LinkedHashSet 2)Hash ...

  5. SVN上传文件提示磁盘空间不足的问题

    SVN上传文件大于100M,提示:Commit failed (details follow):While preparing 'E:\AFCData\tjsc20db_table.sql' for ...

  6. Git命令之资源

    https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA% ...

  7. Schwarz积分公式

    设$f\in H(B(0,R))\cap C(\overline{B(0,R)})$,且$f=u+iv$,则$f$可用其实部表示为 $$f(z)=\frac{1}{2\pi}\int_{0}^{2\p ...

  8. kNN算法python实现和简单数字识别

    kNN算法 算法优缺点: 优点:精度高.对异常值不敏感.无输入数据假定 缺点:时间复杂度和空间复杂度都很高 适用数据范围:数值型和标称型 算法的思路: KNN算法(全称K最近邻算法),算法的思想很简单 ...

  9. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  10. Struts2 日期类型转换

    针对日期类java.util.Date进行类型转换,要求客户端使用"yyyy-MM-dd","yyyy/MM/dd"中的任意一种输入,并以"yyyy- ...