首页 > 移动平台 > 详细

read appSettings in configuration file by XElement with xmlns

时间:2018-12-04 17:51:44      阅读:159      评论:0      收藏:0      [点我收藏+]

 

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

原文:https://www.cnblogs.com/chucklu/p/10065590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!