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