首页 > 代码库 > c#解析xml
c#解析xml
贴代码
xml
<?xml version="1.0" encoding="utf-8" ?> <CoInfo Name="Botanic" Desc="博泰康华项目" Code="BotanicApi"> <SignKey>123321</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/29+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=http://www.mamicode.com/"">有盾服务调用地址身份证</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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。