如何获取LINQ-to-XML处理的元素类型?

时间:2022-10-06 04:15:35

With LINQ I get four elements out of some XML, each element can have a different name (Book, Magazine, Article).

使用LINQ,我从一些XML中获取了四个元素,每个元素都可以有不同的名称(Book,Magazine,Article)。

How do I get the name of the element I'm currently processing, e.g. something like ElementType() below:

如何获取我正在处理的元素的名称,例如类似下面的ElementType():

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 个解决方案

#1


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

XElement.Name是你要追求的吗? (使用XName.LocalName属性,然后获取元素名称的本地部分。)

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...)

如果你能说出你想要的输出,那会有所帮助:)(我原本以为你的意思是节点的类型(属性,元素等),但在你的情况下它总是XElement ...)

#2


You want XElement.Name.

你想要XElement.Name。

#3


Try this:

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

#1


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

XElement.Name是你要追求的吗? (使用XName.LocalName属性,然后获取元素名称的本地部分。)

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...)

如果你能说出你想要的输出,那会有所帮助:)(我原本以为你的意思是节点的类型(属性,元素等),但在你的情况下它总是XElement ...)

#2


You want XElement.Name.

你想要XElement.Name。

#3


Try this:

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