Working with an XML file in C #, I'm trying to convert an XPath query to LINQ, and I don't know how to reach the last section:
XPath:
variable.XPathSelectElements("procedures/menu[@id='value']/procedure[@id]")
LINQ:
from el in variable.Descendants("procedures").Descendants("menu") where el.Element("id").Value == "value"
How do I execute the / procedure [@id] section?
I changed your @Jon sentence, but I seem to be making a simple mistake here that I cannot solve.
XDocument doc = XDocument.Load("procedures.xml"); var query = doc.Elements("procedures") .Elements("menu") .Where(x => (string) x.Attribute("id") == "value") .Elements("procedure").Where(x => x.Attribute("id") != null); public List<string> commands = new List<string>(); foreach (XElement procedure in query) { commands.Add(procedure.Attribute("id")); }
source share