I am trying to use the C# Linq query Descendants method using variables. Here is part of the XML
<CourseList>
<course id="EM220" name="Fundamentals of Mechanics"/>
<course id="EM305" name="Engineering Tools Seminar"/>
<course id="EM320" name="Dynamics"/>
</CourseList>
Here is the query:
static void GetAllCourseIds(XElement doc)
{
IEnumerable<XElement> courseId =
from ci in doc.Descendants("course") <---want to use a variable
select ci;
foreach (XElement ci in courseId)
Console.WriteLine((string)ci.Attribute("id"));
}
I tried to use
string c = "course";
then replaced
from ci in doc.Descendants("course")
with
from ci in doc.Descendants(c)
It would seem easy to do , but obviously not.
any suggestions?