Your code actually works:
static void Main(string[] args)
{
string xml = @"
<Items>
<Item>
<Stuff>Strings</Stuff>
</Item>
<Item>
<Stuff>Strings</Stuff>
</Item>
</Items>";
using (StringReader myStream = new StringReader(xml))
{
XDocument doc = XDocument.Load(myStream);
var query = from node in doc.Descendants(XName.Get("Item"))
select new { Stuff =
node.Element(XName.Get("Stuff")).Value };
foreach (var item in query)
{
Console.WriteLine("Stuff: {0}", item.Stuff);
}
}
It should be noted that if the elements are not qualified with namespaces, then you don't really need XName:
static void Main(string[] args)
{
string xml = @"
<Items>
<Item>
<Stuff>Strings</Stuff>
</Item>
<Item>
<Stuff>Strings</Stuff>
</Item>
</Items>";
using (StringReader myStream = new StringReader(xml))
{
XDocument doc = XDocument.Load(myStream);
var query = from node in doc.Descendants("Item")
select new { Stuff = node.Element("Stuff").Value };
foreach (var item in query)
{
Console.WriteLine("Stuff: {0}", item.Stuff);
}
}
}