c#解析xml

Posted Calabash

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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#解析xml的主要内容,如果未能解决你的问题,请参考以下文章

入坑写代码1——c#解析xml文件

从流输入中解析没有根元素的 XML 片段列表

入坑写代码2——C#解析xml文件续集

求C#解析XML字符串代码

C#解析JSON文件和XML文件(2021.05.26)

C# XML 解析问题