재우니의 블로그


<?xml version="1.0" encoding="utf-8" ?>

<catalog>

  <books>

<book id="bk101"> 

  <author id="1">Gambardella, Matthew</author>

  <title>XML Developer's Guide</title>

  <genre>Computer</genre>

  <price>44.95</price>

  <publish_date>2000-10-01</publish_date>

   <description> An in-depth look at creating applications with XML.</description>

</book>

<book id="bk102">

  <author id="2">Ralls, Kim</author>

  <title>Midnight Rain</title>

  <genre>Fantasy</genre>

  <price>5.95</price>

  <publish_date>2000-12-16</publish_date>

  <description> A former architect battles corporate zombies,an evil sorceress, and her own childhood to become

queen of the world.</description>

</book>

<book id="bk103">

  <author id="3">Corets, Eva</author>

  <title>Maeve Ascendant</title>

  <genre>Fantasy</genre>

    <price>5.95</price>

    <publish_date>2000-11-17</publish_date>

    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for

a new society. </description>

  </book>

 

<book id="bk104">

    <author id="4">Corets, Eva</author>

    <title>Oberon's Legacy</title>

    <genre>Fantasy</genre>

    <price>5.95</price>

    <publish_date>2001-03-10</publish_date>

    <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for

 the inhabitants of London. Sequel to Maeve Ascendant.</description>

 

  </book>

 

<book id="bk105">

    <author id="5">Corets, Eva</author>

    <title>The Sundered Grail</title>

    <genre>Fantasy</genre>

    <price>5.95</price>

    <publish_date>2001-09-10</publish_date>

    <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to 

Oberon's Legacy.</description>

 

  </book>

  </books>

</catalog>



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;


namespace linqtoxml

{

    class Program

    {

        static void Main(string[] args)

        {

            //Exam1();

            //Exam2();

            //Exam3();

            Exam4();


            Console.ReadKey();

                        

        }


        /// <summary>

        /// book 노드의 데이터 전체 담기

        /// </summary>

        static void Exam1()

        {

            XDocument document = XDocument.Load(@"c:\users\shimpark\documents\visual studio 2010\Projects\linqtoxml\linqtoxml\Data.xml");

            var books = from r in document.Descendants("book")

                        select new

                        {

                            Author = r.Element("author").Value,

                            Title = r.Element("title").Value,

                            Genere = r.Element("genre").Value,

                            Price = r.Element("price").Value,

                            PublishDate = r.Element("publish_date").Value,

                            Description = r.Element("description").Value

                        };


            foreach (var r in books)

            {

                Console.WriteLine(r.PublishDate + r.Title + r.Author);

            }

        }


        /// <summary>

        /// 특정 book 의  id 속성에 있는 데이터 담기

        /// </summary>

        static void Exam2()

        {

            //where 조건문을 사용해 봄, book 의 id 의 특정 id 접근하여 객체할당함

            XDocument document = XDocument.Load(@"c:\users\shimpark\documents\visual studio 2010\Projects\linqtoxml\linqtoxml\Data.xml");

            var books = from r in document.Descendants("book")

                        .Where(r=>(string)r.Attribute("id") == "bk102")

                        select new

                        {

                            Author = r.Element("author").Value,

                            Title = r.Element("title").Value,

                            Genere = r.Element("genre").Value,

                            Price = r.Element("price").Value,

                            PublishDate = r.Element("publish_date").Value,

                            Description = r.Element("description").Value

                        };


            foreach (var r in books)

            {

                Console.WriteLine(r.PublishDate + r.Title + r.Author);

            }

        }


        /// <summary>

        /// book 노드의 id 에 접근하여 특정 노드 의 id 속성 값 가져오기

        /// </summary>

        static void Exam3()

        {

            //book 노드의 id 에 접근하여 특정 dom 의 id 값 가져오기

            //노드는 Element, 요소는 Attribute 를 사용한다.

            XDocument document = XDocument.Load(@"c:\users\shimpark\documents\visual studio 2010\Projects\linqtoxml\linqtoxml\Data.xml");

            var books = (from r in document.Descendants("book")

                        .Where(r => (string)r.Attribute("id") == "bk102")

                         select r.Element("author").Attribute("id").Value).FirstOrDefault();


            Console.WriteLine(books);

        }


        /// <summary>

        /// //book 노드에서  author  의 요소의 값을 출력하기

        /// </summary>

        static void Exam4()

        {

            //book 노드에서  author  의 요소의 값을 출력하기

            XDocument document = XDocument.Load(@"c:\users\shimpark\documents\visual studio 2010\Projects\linqtoxml\linqtoxml\Data.xml");

            var books = from r in document.Descendants("book")

                        select r.Element("author").Value;


            foreach (var r in books)

            {

                Console.WriteLine(r.ToString());

            }

        }

    }

}




펌 : http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/reading-xml-file-through-linq-a-few-tips/