tags:

views:

73

answers:

3

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; }
    }

}
+3  A: 

Is XElement.Name what you're after? (Use the XName.LocalName property to then get the local part of the element name.)

If you could say what you want the output to be, that would help :) (I originally thought you meant the type of node (attribute, element etc), but it'll always be XElement in your case...)

Jon Skeet
Yay for silent edits, Mr. XNode.NodeType! :p
Randolpho
@Randolpho: Yes, I originally thought it was the node type that was required, given the repeated use of the word "type". I've included that in a further edit so that anyone who *does* want that still gets to see it. Upvote for your answer as well though :)
Jon Skeet
It's just "Name.ToString()". I was thinking in terms of "Type" as in XmlReader's NodeType, etc. thanks.
Edward Tanguay
I just want the record to show that even the great Jon Skeet makes the occasional mis-parse. Heh... somebody has to bring The Man down... :)
Randolpho
Also, on second read it looks like I might be attacking you, so don't think that. I'm only joking around. It's all in good fun, right? ...right???!?
Randolpho
Plus... it looks like we are now forced to never give you any more rep. 66.6k is just so darn evil, it must continue!
Randolpho
@Randolpho: The answer to stopping me from having 66.6K is to give me *more* rep, surely :)
Jon Skeet
But... we want to keep you at 66.6k.
Randolpho
+1  A: 

Try this:

var contentItems = from contentItem in content.Descendants()
          select new ContentItem
          {
         Id = contentItem.Attribute("id").Value,
         Type = contentItem.Name.LocalName
          };
Andrew Hare
+4  A: 

You want XElement.Name.

Randolpho