• 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="">有盾服务调用地址身份证</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; }
        }
    View Code

    解析

        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;
            }
    
        }
    View Code

    使用

            public static readonly string SignKey = BotanicApiEngine.Instance().SignKey;
            public static readonly string AddressesApi = BotanicApiEngine.Instance().InterfaceOptions["Addresses"].ActionUrl;
  • 相关阅读:
    基于发布/订阅模型的应用程序的主循环设计
    C++使用继承时子对象的内存布局
    安装 CentOS 后的系统配置及软件安装备忘
    环形无锁队列
    并发编程基础
    线程池实现
    Git远程操作
    Unix权限管理
    jquery中,某些写法后来更新导致版本不支持的替代方法
    js相关
  • 原文地址:https://www.cnblogs.com/sunzgod/p/6244118.html
Copyright © 2020-2023  润新知