read appSettings in configuration file by XElement with xmlns

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了read appSettings in configuration file by XElement with xmlns相关的知识,希望对你有一定的参考价值。

 

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-write-queries-on-xml-in-namespaces

public static int GetProgramId(string filePath)
        {
            int programId = 0;
            var element = XElement.Load(filePath);

            XNamespace xNamespace = element.GetDefaultNamespace();

            XName tempXName1 = xNamespace + "appSettings";
            var temp = element.Element(tempXName1);

            XName tempXName2 = xNamespace + "add";
            var targetElement = temp?.Elements(tempXName2)
                .FirstOrDefault(x => x.Attribute("key")?.Value == "ProgramID");
            if (targetElement == null)
            {
                LogUtil.CreateLog(LogLevel.Error, $"Can not find ProgramID in appSettings section in {filePath}");
            }
            else
            {
                var valueAttribute = targetElement.Attribute("value");
                if (valueAttribute == null)
                {
                    LogUtil.CreateLog(LogLevel.Error,
                        $"Can not find value attribute in appSettings section with key = ProgramID in {filePath}");
                }
                else
                {
                    programId = Convert.ToInt32(valueAttribute.Value);
                }
            }

            return programId;
        }

 

以上是关于read appSettings in configuration file by XElement with xmlns的主要内容,如果未能解决你的问题,请参考以下文章

加密 App.Config 文件的 appsettings 部分中的单个密钥

Talking appsettings.json in Asp.Net Core

How to configure spring boot through annotations in order to have something similar to <jsp-confi

springcloud本地启动指定profile后错误If you are using the git profile, you need to set a Git URI in your confi

如何高逼格读取Web.config中的AppSettings

读取web.config和app.config配置文件