次の方法で共有


XPathNavigator を使用して XML データを抽出する

Microsoft .NET Framework で XML ドキュメントを表すには、いくつかの方法があります。 これには、 Stringの使用、または XmlReaderXmlWriterXmlDocument、または XPathDocument クラスの使用が含まれます。 XML ドキュメントのこれらの異なる表現間の移動を容易にするために、XPathNavigator クラスには、、 オブジェクト、または オブジェクトとして XML を抽出するためのさまざまなメソッドとプロパティが用意されています。

XPathNavigator を文字列に変換する

OuterXml クラスの XPathNavigator プロパティは、XML ドキュメント全体のマークアップ、または単一ノードとその子ノードのマークアップのみを取得するために使用されます。

InnerXml プロパティは、ノードの子ノードのマークアップのみを取得します。

次のコード例は、 XPathNavigator オブジェクトに含まれる XML ドキュメント全体を 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に設定されます。 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 ファイルを入力として受け取ります。

こちらも参照ください