Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to XML query attributes

Tags:

xml

linq

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

like image 971
kb. Avatar asked Apr 07 '10 08:04

kb.


People also ask

Does LINQ work with XML?

The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes.

Which of the following option is created to retrieve data into XML using LINQ?

The LINQ to XML will bring the XML document into memory and allows us to write LINQ Queries on in-memory XML document to get the XML document elements and attributes. To use LINQ to XML functionality in our applications, we need to add "System. Xml. Linq" namespace reference.

What is an XML element attribute vs element?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements.

Which of the following link class is used to query against XML?

IEnumerable<T> can be used to query the XML data and for this standard query the operators show up as extension methods on any object that implements IEnumerable<T> and can be invoked like any other method.


2 Answers

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

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

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

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

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")
             };
like image 190
Jon Skeet Avatar answered Sep 26 '22 15:09

Jon Skeet


Change this line:

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

To:

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

Otherwise you're comparing the attribute as object instead of the attribute's value.

like image 21
Prutswonder Avatar answered Sep 23 '22 15:09

Prutswonder