With LINQ I get four elements out of some XML, each element can have a different name (Book, Magazine, Article).
How do I get the name of the element I'm currently processing, e.g. something like ElementType() below:
using System;
using System.Linq;
using System.Xml.Linq;
namespace TestXmlElement2834
{
class Program
{
static void Main(string[] args)
{
XElement content = new XElement("content",
new XElement("book", new XAttribute("id", "1")),
new XElement("article", new XAttribute("id", "2")),
new XElement("book", new XAttribute("id", "3")),
new XElement("magazine", new XAttribute("id", "4"))
);
var contentItems = from contentItem in content.Descendants()
select new ContentItem
{
Id = contentItem.Attribute("id").Value
Type = contentItem.ElementType() //PSEUDO-CODE
};
foreach (var contentItem in contentItems)
{
Console.WriteLine(contentItem.Id);
}
Console.ReadLine();
}
}
class ContentItem
{
public string Id { get; set; }
public string Type { get; set; }
}
}