XMLDocument 이용한 검색

 

string path = "booklist.xml";

XmlDocument doc = new XmlDocument();

doc.Load(path);

string xPath = "//book/title[../@kind='소설']";

// SelectNodes 메서드는 검색조건에 맞는 개체를 XmlNodeList 반환하고

// SelectSingleNode 메서드는 검색조건에 맞는 단한개의 XmlNode 만을 반환(ID속성일 경우 쓰임)

           

XmlNodeList nodeList = doc.SelectNodes(xPath);

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

{

string title = nodeList[i].InnerText;

Console.WriteLine(title);

}


 

XPathDocument 이용한 검색

 // [1]. XPathDocument 생성

XPathDocument xdoc = new XPathDocument("booklist.xml");

// [2]. XPathNavigator 생성 -> XML 데이터를 탐색하고 편집하기 위한 커서모델을 제공한다.

XPathNavigator xPathNavi= xdoc.CreateNavigator();

// [3]. XPathNodeIterator 생성 -> 반복기 역활

XPathNodeIterator xPathIterator = xPathNavi.Select("//booklist/book/title[../@kind='컴퓨터']");

// [4]. kind='컴퓨터' 없을 까지 돈다.

while (xPathIterator.MoveNext())

{

     XPathNavigator navigatorTitle = xPathIterator.Current;

     Console.WriteLine(navigatorTitle.Value);

}  



by 피요히코~ 2009. 2. 25. 13:15

XML 에서 속성 가져오는 방법..

            string path = "booklist.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlElement booklist=doc.DocumentElement;
            XmlElement FirstBook = (XmlElement)booklist.FirstChild;

            // 1. XmlAttributeCollection 를 사용해서 속성 가져오기 
            XmlAttributeCollection attributes = FirstBook.Attributes;
            for (int i = 0; i < attributes.Count; i++)
            {
                XmlAttribute attribute = (XmlAttribute)attributes[i];
                Console.WriteLine(attribute.Name + ":" + attribute.Value);
            }

            // 2. GetAttributd() 메서드를 이용해서 속성 가져오기
            string id = FirstBook.GetAttribute("id");
            Console.WriteLine("id:"+id);
            string kind = FirstBook.GetAttribute("kind");
            Console.WriteLine("kind:"+kind);
        }



 

XML 에서 모든엘리먼트 가져오기
 
           string path = "booklist.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlElement booklist = doc.DocumentElement;

          
            //// GetElementsByTagName() 으로 엘리먼트 가져오기
            XmlNodeList titleNodeList = doc.GetElementsByTagName("price");
            for (int i = 0; i < titleNodeList.Count; i++)
            {
                XmlNode titleNode = titleNodeList[i];
                XmlNodeList childNodeList = titleNode.ChildNodes;
                XmlNode textNode = childNodeList[0];
                string value = textNode.Value;
                Console.WriteLine(value);
            }



by 피요히코~ 2009. 2. 25. 13:14
string filePath = "booklist.xml";

XmlDocument doc = new XmlDocument();

doc.Load(filePath);

// 잘짜인 xml 이면은 반드시 한개의 엘리먼트가 있기때문에 XmlDocument 개체의 속성인 DocumentElement 바로 얻을 있다.

 

XmlElement booklist = doc.DocumentElement; 

 

// 첫번쨰 자식엘리먼트의 노드들의 수를 가져온다.

XmlNodeList childs = FirstBook.ChildNodes;

 

// 첫번쨰 자식엘리먼트의 카운트수 만큼 노드의 이름 내용을 보여준다.            

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

{

       XmlElement eChild = (XmlElement)childs[i];

      Console.WriteLine(eChild.Name + ":" + eChild.InnerText);

 }


by 피요히코~ 2009. 2. 25. 13:14
| 1 |