Looking at your code and comments about what you are trying to do.
Indeed your code seems to be okay, your XPath value is the one problem based on your input XML and XPath value.
You could try this sample code it works.
using System;
using System.Xml.Linq;
using System.Xml.XPath;
namespace TestConsoleApp
{
public class Program
{
private static void Main(string[] args)
{
string XML = $"<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration><appSettings><!--keys specific to auto-publish--><add key=\"abc\" value=\"xyz\" /><add key=\"abcd\" value=\"pqr\" /><add key=\"abce\" value=\"/Building%20Blocks/Content/def.xml\" /></appSettings></configuration>";
XDocument document = XDocument.Parse(XML);
foreach (XElement element in document.Root.XPathSelectElements("/configuration/appSettings/add"))
{
string key = element.Attribute("key") != null ? element.Attribute("key").Value : string.Empty;
string value = element.Attribute("value") != null ? element.Attribute("value").Value : string.Empty;
Console.WriteLine($"Key: {key}, Value: {value}");
}
Console.ReadKey();
}
}
}
I hope it helps.