LINQ to XML查询属性 [英] LINQ to XML query attributes

查看:61
本文介绍了LINQ to XML查询属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

using LINQ to XML, this is a sample of my 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"),并希望将所有日期/时间作为列表返回.

How do I return a list of the Dates for a specfic show? I am passing the show code ("456") in the querystring and want all the dates/times returned as a list.

这是我到目前为止的代码:

This is the code i have so far:

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 />");
            }

但是我没有返回任何结果

But i get no results returned

推荐答案

该属性不会等于等于"456"-它是一个属性,而不是字符串.但是,如果先将其转换为字符串,它将可以使用.

The attribute isn't going to equal "456" - it's an attribute, not a string. However, if you convert it to a string first, it will work.

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

另一种选择是int以确保它是数字:

An alternative would be to int to make sure it's numeric:

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

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

Okay, I've now got this as a short but complete program to show it working.

请注意,只有在显示"元素具有日期"属性的情况下,原始代码才有效-该属性在示例XML中不存在.请注意,它试图从显示" 元素中获取日期",而不是从"Event" 元素中获取.我不确定您在这里真正想做什么,所以我将代码更改为只强制转换为DateTime?.下面的代码可以工作并打印1(即找到与代码匹配的单个Show元素):

Note that your original code will only work if the "Show" element has a "Date" attribute - which it doesn't in your sample XML. Note that it's trying to take the "Date" from the "Show" element not the "Event" element. I'm not sure what you really wanted to do here, so I've changed the code to just cast to DateTime? instead. The code below works and prints 1 (i.e. it's found the single Show element matching the code):

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"子句以使其遍历节目中的事件:

If you're actually trying to find every event date within the show, you need another "from" clause to make it iterate over the events within the show:

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

这篇关于LINQ to XML查询属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆