LINQ to XML query attributes

后端 未结 3 448
一整个雨季
一整个雨季 2021-01-13 04:30

using LINQ to XML, this is a sample of my XML


 
   

        
相关标签:
3条回答
  • 2021-01-13 05:07

    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.

    0 讨论(0)
  • 2021-01-13 05:14

    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")
                 };
    
    0 讨论(0)
  • 2021-01-13 05:14

    Here's another way of doing it:

       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"));
    }   
    
    0 讨论(0)
提交回复
热议问题