Microsoft .NET Framework에서 XML 문서를 나타내는 방법에는 여러 가지가 있습니다. 여기에는 String을(를) 사용하거나, XmlReader, XmlWriter, XmlDocument, 또는 XPathDocument 클래스를 사용하는 것이 포함됩니다. XML 문서의 이러한 다양한 표현 간에 쉽게 이동할 수 있도록, XPathNavigator 클래스는 XML을 String 개체, XmlReader 개체 또는 XmlWriter 개체로 추출하기 위한 다양한 메서드와 속성을 제공합니다.
XPathNavigator를 문자열로 변환
클래스의 OuterXml 속성은 XPathNavigator 전체 XML 문서의 태그 또는 단일 노드 및 해당 자식 노드의 태그를 가져오는 데 사용됩니다.
비고
이 속성은 InnerXml 노드의 자식 노드에 대한 태그만 가져옵니다.
다음 코드 예제에서는 전체 XML 문서를 XPathNavigator 객체에 String로 저장하는 방법, 단일 노드와 그 자식 노드의 저장 방법을 보여 줍니다.
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설정됩니다. 개체의 XmlReaderRead 메서드가 처음 호출되면, XmlReader가 XPathNavigator의 현재 노드로 이동합니다. 새 XmlReader 개체는 XML 트리의 끝에 도달할 때까지 계속 읽습니다. 이 시점에서 Read 메서드가 false
을 반환하고, XmlReader 객체의 ReadState 속성이 EndOfFile로 설정됩니다.
XPathNavigator 개체의 위치는 개체의 생성 또는 이동 XmlReader 에 의해 변경되지 않습니다. 이 ReadSubtree 메서드는 요소 또는 루트 노드에 배치된 경우에만 유효합니다.
다음 예제에서는 단일 노드 및 그 자식 노드를 포함한 전체 XML 문서를 XmlReader 개체에 포함하여 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 개체의 생성으로 인해 변하지 않습니다.
다음 예제에서는 단일 노드 및 그 자식 노드를 포함한 전체 XML 문서를 XmlWriter 개체에 포함하여 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
있는 파일을 입력으로 사용합니다.
참고하십시오
.NET