프로그래밍/.NET2013. 2. 9. 21:59

<?xml version="1.0"?>

<Data>

<Products>

<Product Name="West Side Story" Price="9.99" SupplierID="1" />

<Product Name="Assassins" Price="14.99" SupplierID="2" />

<Product Name="Frogs" Price="13.99" SupplierID="1" />

<Product Name="Sweeney Todd" Price="10.99" SupplierID="3" />

</Products>


<Suppliers>

<Supplier Name="Solely Sondheim" SupplierID="1" />

<Supplier Name="CD-by-CD-by-Sondheim" SupplierID="2" />

<Supplier Name="Narnershop CDs" SupplierID="3" />

</Suppliers>

</Data>

XML 데이터


Program.cs

    class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("data.xml");
            var filtered = from p in doc.Descendants("Product")
                           join s in doc.Descendants("Supplier")
                           on (int)p.Attribute("SupplierID")
                           equals (int)s.Attribute("SupplierID")
                           where (decimal)p.Attribute("Price") > 10
                           orderby (string)s.Attribute("Name"), (string)p.Attribute("Name")
                           select new
                           {
                               SupplierName = (string)s.Attribute("Name"),
                               ProductName = (string)p.Attribute("Name")
                           };

            foreach (var v in filtered)
            {
                Console.WriteLine("Supplier={0}, Product={1}",
                    v.SupplierName, v.ProductName);
            }

            Console.ReadLine();
        }
    }




'프로그래밍 > .NET' 카테고리의 다른 글

[C#]대리자 Func<> / Action<>  (0) 2013.02.10
[C#]Delegate  (0) 2013.02.10
[C#]const, readonly 상수  (1) 2013.02.09
[C#]DataTable -> CSV 변환  (0) 2013.02.07
[C#, MySQL]CSV파일의 데이터를 DB Export  (0) 2013.02.07
Posted by 건깡