Query attribute value

You can also use casts in LINQ queries.

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        var data = XElement.Parse(@"<data><customer id='1' name='A' credit='100' /><customer id='2' name='B' credit='150' /><customer id='3' name='C' /></data>");

        IEnumerable<string> query = from cust in data.Elements()
                                    where (int?)cust.Attribute("credit") > 100
                                    select cust.Attribute("name").Value;
        foreach(string s in query){
           Console.WriteLine(s);
        
        }
    }
}

The output:


B
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.