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);
}
}
}
}