I\'m wondering whether there is a way using C# which enables me to return all the inner values within an XML file matching a given XPath query.
Let\'s suppose that we ha
One way to do this is to use System.Xml.XPath.Extensions.XPathEvaluate
.
E.g.
string xmlFilePath = "exampleWithFruits.xml";
string xPathQuery = "//fruits/apples//@color";
var doc = XDocument.Load(xmlFilePath);
IEnumerable att = (IEnumerable)doc.XPathEvaluate(xPathQuery);
string[] matchingValues = att.Cast<XAttribute>().Select(x => x.Value).ToArray();
Or if you prefer XmlDocument:
var doc = new XmlDocument();
doc.Load(xmlFilePath);
string[] matchingValues = doc.SelectNodes(xPathQuery).Cast<XmlAttribute>().Select(x => x.Value).ToArray();