XML select

XmlDocument ¿¡¼­ ƯÁ¤ XmlNode¸¦ °Ë»öÇÒ ¶§, GetElementsByTagName ¸Þ¼Òµå ÀÌ¿Ü¿¡ SelectNodes, SelectSingleNode ¸Þ¼Òµå·Îµµ ƯÁ¤ ³ëµå¸¦ ãÀ»¼ö ÀÖ´Ù.

¿©±â¼­, SelectNodes, SelectSingleNode µÎ °³ÀÇ ¸Þ¼Ò¿¡ ´ëÇؼ­ ¼³¸íÇÏ°Ù´Ù.
SQL ±¸¹®¿¡¼­ °Ë»ö½Ã select Äõ¸®¹®À» ÀÌ¿ëÇϴµ¥, À̸§ÀÌ ºñ½ÁÇÏ´Ù.

SelectNodes, SelectSingle Ư¡

SelectNodes : ÇØ´çÇÏ´Â XmlNodes ¸ñ·ÏÀÎ XmlNodeList¸¦ ¹ÝȯÇÑ´Ù.
SelectSingle :  ÇØ´çÇÏ´Â XmlNode¸¦ ¹ÝȯÇÑ´Ù.

´ÙÀ½À» list.xml  ÆÄÀÏÀ» ·ÎµùÇؼ­ °Ë»öÇÏ´Â ¿¹Á¦ÀÌ´Ù.

list.xml

<?xml version="1.0" encoding="utf-8" ?>
<food_list>
<food ID="001" kind="ÇѽÄ" >
  <cook>Çѿ丮</cook>
  <name>±èÄ¡</name>
</food>
<food ID="002" kind="ÀϽÄ">
  <cook>¿Í¸¶½Ãµû</cook>
  <name>Ãʹä</name>
</food>
<food ID="003" kind="ÇѽÄ">
  <cook>±èÄ¡±¹</cook>
  <name>Ãß¾îÅÁ</name>
</food>
</food_list>

 

using System;

using System.IO;

using System.Xml;

 

namespace xml

{

    class Program

    {

        static void Main()

        {

            string filePath = @"..\..\list.xml";

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(filePath);

 

            XmlNodeList xmlNode = xmlDoc.GetElementsByTagName("food");

            for (int i = 0; i < xmlNode.Count; i++)

            {

                //¼Ó¼º Ãâ·Â

                XmlAttributeCollection xmlAttr = xmlNode[i].Attributes;

                for (int j = 0; j < xmlAttr.Count; j++)

                {

                    Console.Write(xmlAttr[j].Name);

                    Console.Write(": " + xmlAttr[j].Value + "\t\t");

                }

                Console.WriteLine("");

 

                //µ¥ÀÌŸ Ãâ·Â

                XmlElement data = (XmlElement)xmlNode[i].FirstChild;

                while (data != null)

                {

                    Console.Write(data.Name);

                    Console.WriteLine(":\t" + data.InnerText);

                    data = (XmlElement)data.NextSibling;

                }

                /*

                                XmlNodeList xmlChild = xmlNode[i].ChildNodes;

                                for (int k = 0; k < xmlChild.Count; k++)

                                {

                                    XmlElement data = (XmlElement)xmlChild[k];

                                    Console.Write(data.Name);

                                    Console.WriteLine(":\t" + data.InnerText);

                                }

                */

 

            }

 

            //SelectNodes ¿¹Á¦par             Console.WriteLine("\n---------------- SelectNodes ¿¹Á¦----------------");

            {

                XmlElement root = xmlDoc.DocumentElement;

                XmlNodeList nodeList = root.SelectNodes("/food_list/food/@kind");

                foreach (XmlNode food in nodeList)

                    Console.WriteLine(food.Value);

            }

            //SelectNodes ¿¹Á¦par             {

                Console.WriteLine("\n---------------- SelectNodes ¿¹Á¦----------------");

                XmlElement root = xmlDoc.DocumentElement;

                XmlNodeList nodeList = root.SelectNodes("/food_list/food");

                foreach (XmlNode food in nodeList)

                    Console.WriteLine(food.OuterXml);

            }

            //SelectSingleNode ¿¹Á¦

            {

                Console.WriteLine("\n---------------- SelectSingleNode ¿¹Á¦ ----------------");

                XmlNode food = xmlDoc.SelectSingleNode("descendant::food[@ID='003' and @kind='ÇѽÄ']");

                Console.WriteLine(food.OuterXml);

            }

        }

    }

}

- SelectNodes ¿¹Á¦1
°Ë»ö Äõ¸®:   /food_list/food/@kind
¼³¸í: food_list ÇÏÀ§¿¡ Á¸ÀçÇÏ´Â foodÀÇ kind Attribute , @´Â AttributeÀ» ÀǹÌÇÑ´Ù.

- SelectNodes ¿¹Á¦2
°Ë»ö Äõ¸® :  /food_list/food
¼³¸í : food_list ÇÏÀ§¿¡ Á¸ÀçÇÏ´Â foodÀÇ ¸®½ºÆ®

- SelectSingleNode ¿¹Á¦
°Ë»ö Äõ¸® :  descendant::food[@ID='003' and @kind='ÇѽÄ']
¼³¸í : foodÀÇ ÇÏÀ§¿¡ Á¸ÀçÇÏ´Â ID Attribute°¡ 003ÀÌ°í kind Attribute°¡ ÇѽÄ

- ½ÇÇà °á°ú

ID: 001         kind: ÇѽÄ
cook:   Çѿ丮
name:   ±èÄ¡
ID: 002         kind: ÀϽÄ
cook:   ¿Í¸¶½Ãµû
name:   Ãʹä
ID: 003         kind: ÇѽÄ
cook:   ±èÄ¡±¹
name:   Ãß¾îÅÁ

---------------- SelectNodes ¿¹Á¦1 ----------------
ÇѽÄ
ÀϽÄ
ÇѽĠ

---------------- SelectNodes ¿¹Á¦2 ----------------
<food ID="001" kind="ÇѽÄ"><cook>Çѿ丮</cook><name>±èÄ¡</name></food>
<food ID="002" kind="ÀϽÄ"><cook>¿Í¸¶½Ãµû</cook><name>Ãʹä</name></food>
<food ID="003" kind="ÇѽÄ"><cook>±èÄ¡±¹</cook><name>Ãß¾îÅÁ</name></food>

---------------- SelectSingleNode ¿¹Á¦ ----------------
<food ID="003" kind="ÇѽÄ"><cook>±èÄ¡±¹</cook><name>Ãß¾îÅÁ</name></food>

 

 

¼Ò½º : xml_select.zip