xml - LINQ to XML 查询属性

标签 xml linq

使用 LINQ to XML,这是我的 XML 示例

<shows>
 <Show Code="456" Name="My Event Name">
   <Event Code="2453" VenueCode="39" Date="2010-04-13 10:30:00" /> 
   <Event Code="2454" VenueCode="39" Date="2010-04-13 13:30:00" /> 
   <Event Code="2455" VenueCode="39" Date="2010-04-14 10:30:00"  /> 
   <Event Code="2456" VenueCode="39" Date="2010-04-14 13:30:00" /> 
   <Event Code="2457" VenueCode="39" Date="2010-04-15 10:30:00" /> 
 </Show>

 <Show... />
 <Show... />
</shows>

如何返回特定节目的日期列表?我在查询字符串中传递显示代码(“456”)并希望所有日期/时间作为列表返回。

这是我目前的代码:

XDocument xDoc = XDocument.Load("path to xml");

            var feeds = from feed in xDoc.Descendants("Show")
                        where feed.Attribute("Code").Equals("456")
                        select new
                        {
                            EventDate = feed.Attribute("Date").Value
                        };

            foreach(var feed in feeds)
            {
                Response.Write(feed.EventDate + "<br />");
            }

但是我没有返回任何结果

最佳答案

该属性不会等于“456”——它是一个属性,而不是一个字符串。但是,如果您先将其转换为字符串,它就会起作用。

var feeds = from feed in xDoc.Descendants("Show")
            where (string)feed.Attribute("Code") == "456"
            select new
            {
                EventDate = feed.Attribute("Date").Value
            };

另一种方法是 int 以确保它是数字:

var feeds = from feed in xDoc.Descendants("Show")
            where (int) feed.Attribute("Code") == 456
            select new
            {
                EventDate = feed.Attribute("Date").Value
            };

编辑:好的,我现在把它作为一个简短但完整的程序来展示它的工作原理。

请注意,只有当“Show”元素具有“Date”属性时,您的原始代码才有效——它在您的示例 XML 中没有。请注意,它试图从 “显示” 元素而不是 “事件” 元素中获取“日期”。我不确定您在这里真正想做什么,所以我将代码更改为仅转换为 DateTime?。下面的代码有效并打印 1(即找到与代码匹配的单个 Show 元素):

using System;
using System.Linq;
using System.Xml.Linq;

public static class Test
{    
    static void Main(string[] args)
    {
        XDocument xDoc = XDocument.Load("shows.xml");
        var feeds = from feed in xDoc.Descendants("Show")
                    where (int) feed.Attribute("Code") == 456
                    select new
                    {
                        EventDate = (DateTime?) feed.Attribute("Date")
                    };

        Console.WriteLine(feeds.Count());
    }
}

如果您实际上试图在节目中查找每个事件日期,您需要另一个“from”子句来使其遍历节目中的事件:

var events = from feed in xDoc.Descendants("Show")
             where (int) feed.Attribute("Code") == 456
             // We can't use event as an identifier, unfortunately
             from ev in feed.Elements("Event")
             select new
             {
                 EventDate = (DateTime?) ev.Attribute("Date")
             };

关于xml - LINQ to XML 查询属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2591096/

相关文章:

Python 3.x : parse ATOM XML and convert to dict

xml - XSLT 将 2 个 XML 文件作为输入并生成输出 XML 文件

xml - 如何强制子元素在 XSD 中具有值?

.net - IEnumerable<> 上的 First() 和 Last() 真的是第一个和最后一个吗?

c# - 内部列表上的 Entity Framework 过滤器

c# - 为什么没有 where 子句的查询会出现输入字符串异常?

javascript - 一次加载全部 XML 还是一点一点加载更快?

c# - 为什么 var 在 XmlNodeList 循环中推断类型对象而不是 XmlNode?

c# - 如何使用 LINQ 仅返回列的第一个字符在某个范围内的行?

.net - 通过非泛型 IDictionary 枚举时,无法将泛型字典项转换为 DictionaryEntry