본문 바로가기

C#

XmlDocument 사용법 정리


XML 문서

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <attract>
    <time type="bboy" sp="0:0:0" ep="0:0:13"/>
    <time type="km" sp="0:0:13" ep="0:0:32"/>
    <time type="bboy" sp="0:0:32" ep="0:0:55"/>
    <time type="magic" sp="0:0:55" ep="0:1:27"/>
    <time type="pid" sp="0:1:27" ep="0:1:40"/>
    <time type="km" sp="0:1:40" ep="0:1:55"/>
    <time type="bboy" sp="0:1:55" ep="0:2:13"/>
    <time type="km" sp="0:2:13" ep="0:2:36"/>
    <time type="magic" sp="0:2:36" ep="0:3:00"/>
    <time type="pid" sp="0:3:00" ep="0:3:19"/>
  </attract>
</root>



c#

attractTimeTable = new List<KBVideoItem>();
            XmlDocument xml = new XmlDocument();
            xml.Load(Environment.CurrentDirectory + "/config/KBVideoTimeTable.xml");


            XmlNodeList _attract_list = xml.SelectNodes("/root/attract/time");
            Console.WriteLine(_attract_list.Count);
            foreach (XmlNode xn in _attract_list)
            {
                Console.WriteLine("----------------------------");
                Console.WriteLine(xn.Attributes["type"].InnerText);
                Console.WriteLine(xn.Attributes["sp"].InnerText);
                Console.WriteLine(xn.Attributes["ep"].InnerText);
            }