다음을 통해 공유


XPathNavigator를 사용하여 XML 데이터 추출

Microsoft .NET Framework에서는 여러 가지 방법으로 XML 문서를 나타낼 수 있습니다. 예를 들어, String을 사용하거나 XmlReader, XmlWriter, XmlDocument 또는 XPathDocument 클래스를 사용하여 XML 문서를 나타낼 수 있습니다. 여러 가지 XML 문서 표현 간을 이동할 수 있도록 XPathNavigator 클래스는 XML을 String, XmlReader 개체 또는 XmlWriter 개체로 추출하는 많은 메서드와 속성을 제공합니다.

XPathNavigator를 문자열로 변환

OuterXml 클래스의 XPathNavigator 속성을 사용하여 전체 XML 문서의 태그를 가져오거나 단일 노드 및 해당 자식 노드의 태그만 가져올 수 있습니다.

참고 항목

InnerXml 속성은 노드의 자식 노드의 태그만 가져옵니다.

다음 코드 예제에서는 XPathNavigator 개체에 String으로 포함된 전체 XML 문서 및 단일 노드와 자식 노드를 저장하는 방법을 보여 줍니다.

Dim document As XPathDocument = New XPathDocument("input.xml")  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
' Save the entire input.xml document to a string.  
Dim xml As String = navigator.OuterXml  
  
' Now save the Root element and its child nodes to a string.  
navigator.MoveToChild(XPathNodeType.Element)  
Dim root As String = navigator.OuterXml  
XPathDocument document = new XPathDocument("input.xml");  
XPathNavigator navigator = document.CreateNavigator();  
  
// Save the entire input.xml document to a string.  
string xml = navigator.OuterXml;  
  
// Now save the Root element and its child nodes to a string.  
navigator.MoveToChild(XPathNodeType.Element);  
string root = navigator.OuterXml;  

XPathNavigator를 XmlReader로 변환

ReadSubtree 메서드를 사용하면 XML 문서의 전체 내용이나 단일 노드 및 해당 자식 노드만 XmlReader 개체로 스트림할 수 있습니다.

현재 노드 및 자식 노드를 사용하여 XmlReader 개체를 만든 경우 XmlReader 개체의 ReadState 속성은 Initial로 설정됩니다. XmlReader 개체의 Read 메서드를 처음으로 호출하면 XmlReaderXPathNavigator의 현재 노드로 이동합니다. 새 XmlReader 개체는 XML 트리 끝에 도달할 때까지 계속 읽습니다. 이때 Read 메서드는 false를 반환하고 XmlReader 개체의 ReadState 속성은 EndOfFile로 설정됩니다.

XPathNavigator 개체를 만들거나 이동해도 XmlReader 개체의 위치는 변경되지 않습니다. ReadSubtree 메서드는 요소 또는 루트 노드에 있을 경우에만 유효합니다.

다음 예제에서는 XmlReader 개체의 전체 XML 문서를 포함하는 XPathDocument 개체뿐만 아니라 단일 노드 및 해당 자식 노드도 가져오는 방법을 보여 줍니다.

Dim document As XPathDocument = New XPathDocument("books.xml")  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
' Stream the entire XML document to the XmlReader.  
Dim xml As XmlReader = navigator.ReadSubtree()  
  
While xml.Read()  
    Console.WriteLine(xml.ReadInnerXml())  
End While  
  
xml.Close()  
  
' Stream the book element and its child nodes to the XmlReader.  
navigator.MoveToChild("bookstore", "")  
navigator.MoveToChild("book", "")  
  
Dim book As XmlReader = navigator.ReadSubtree()  
  
While book.Read()  
    Console.WriteLine(book.ReadInnerXml())  
End While  
  
book.Close()  
XPathDocument document = new XPathDocument("books.xml");  
XPathNavigator navigator = document.CreateNavigator();  
  
// Stream the entire XML document to the XmlReader.  
XmlReader xml = navigator.ReadSubtree();  
  
while (xml.Read())  
{  
    Console.WriteLine(xml.ReadInnerXml());  
}  
  
xml.Close();  
  
// Stream the book element and its child nodes to the XmlReader.  
navigator.MoveToChild("bookstore", "");  
navigator.MoveToChild("book", "");  
  
XmlReader book = navigator.ReadSubtree();  
  
while (book.Read())  
{  
    Console.WriteLine(book.ReadInnerXml());  
}  
  
book.Close();  

이 예제에서는 books.xml 파일을 입력으로 사용합니다.

<?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

XPathNavigator를 XmlWriter로 변환

WriteSubtree 메서드를 사용하면 XML 문서의 전체 내용이나 단일 노드 및 해당 자식 노드만 XmlWriter 개체로 스트림할 수 있습니다.

XPathNavigator 개체를 만들어도 XmlWriter 개체의 위치는 변경되지 않습니다.

다음 예제에서는 XmlWriter 개체의 전체 XML 문서를 포함하는 XPathDocument 개체뿐만 아니라 단일 노드 및 해당 자식 노드도 가져오는 방법을 보여 줍니다.

Dim document As XPathDocument = New XPathDocument("books.xml")  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
' Stream the entire XML document to the XmlWriter.  
Dim xml As XmlWriter = XmlWriter.Create("newbooks.xml")  
navigator.WriteSubtree(xml)  
xml.Close()  
  
' Stream the book element and its child nodes to the XmlWriter.  
navigator.MoveToChild("bookstore", "")  
navigator.MoveToChild("book", "")  
  
Dim book As XmlWriter = XmlWriter.Create("book.xml")  
navigator.WriteSubtree(book)  
book.Close()  
XPathDocument document = new XPathDocument("books.xml");  
XPathNavigator navigator = document.CreateNavigator();  
  
// Stream the entire XML document to the XmlWriter.  
XmlWriter xml = XmlWriter.Create("newbooks.xml");  
navigator.WriteSubtree(xml);  
xml.Close();  
  
// Stream the book element and its child nodes to the XmlWriter.  
navigator.MoveToChild("bookstore", "");  
navigator.MoveToChild("book", "");  
  
XmlWriter book = XmlWriter.Create("book.xml");  
navigator.WriteSubtree(book);  
book.Close();  

이 예제에서는 이 항목의 앞부분에 있는 books.xml 파일을 입력으로 사용합니다.

참고 항목