LINQ to XML Query Attributes

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>

How do I return a list of dates for speculation? I pass the show code ("456") in the query string and want all dates / times to be returned as a list.

This is the code that I still have:

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

+5
source share
3 answers

The attribute will not be equal to "456" - this is 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
            };

An alternative would be intto make sure it is numeric:

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

EDIT: Okay, now I have this short but complete program to show how it works.

, , "" "", XML. , "" "" , "". , , , DateTime?. 1 (.. , "" ):

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

:

where feed.Attribute("Code").Equals("456")

To:

where feed.Attribute("Code").Value.Equals("456")

.

+6

:

   var nodes = xmlFile.Nodes()
  .OfType<XElement>()
    .DescendantNodes()
  .OfType<XElement>()
    .Where(x => x.Name.LocalName == "Event" && x.Attribute("Code").Value.Contains("456"));
foreach (var node in nodes)
{
    Console.WriteLine(node.Attribute("Code"));
}   
+2

All Articles