XPathNavigator による XML データの挿入

XPathNavigator クラスは、XML ドキュメント内に兄弟ノード、子ノード、および属性ノードを挿入するためのメソッドのセットを提供します。 これらのメソッドを使用するには、XPathNavigator オブジェクトが編集可能である必要があります。つまり、その CanEdit プロパティを true にする必要があります。

XML ドキュメントを編集できる XPathNavigator オブジェクトは、CreateNavigator クラスの XmlDocument メソッドによって作成されます。 XPathNavigator クラスによって作成される XPathDocument オブジェクトは読み取り専用であり、XPathNavigator オブジェクトによって作成される XPathDocument オブジェクトの編集メソッドを使用しようとすると、NotSupportedException が発生します。

編集可能な XPathNavigator オブジェクトの作成方法については、「XPathDocument および XmlDocument を使用した XML データの読み取り」を参照してください。

ノードの挿入

XPathNavigator クラスは、XML ドキュメント内に兄弟ノード、子ノード、および属性ノードを挿入するためのメソッドを提供します。 これらのメソッドによって、XPathNavigator オブジェクトの現在位置に関連した異なる場所にノードまたは属性を挿入できます。これらのメソッドについて以下に説明します。

兄弟ノードの挿入

XPathNavigator クラスは、兄弟ノードを挿入する次のメソッドを提供します。

これらのメソッドは XPathNavigator オブジェクトの現在位置にあるノードの前と後に兄弟ノードを挿入します。

InsertAfter および InsertBefore メソッドは、オーバーロードされ、stringXmlReader オブジェクト、または追加する兄弟ノードをパラメーターとして含む XPathNavigator オブジェクトを受け取ります。 両方のメソッドは、兄弟ノードの挿入に使用される XmlWriter オブジェクトも返します。

InsertElementAfter および InsertElementBefore メソッドは、パラメーターとして指定された名前空間プレフィックス、ローカル名、名前空間 URI、および値を使用して XPathNavigator オブジェクトの現在位置にあるノードの前と後に、1 つの兄弟ノードを挿入します。

次の例では、新しい pages 要素が price ファイル内の最初の book 要素の contosoBooks.xml 子要素の前に挿入されます。

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");

navigator->InsertBefore("<pages>100</pages>");

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");

navigator.InsertBefore("<pages>100</pages>");

navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")

navigator.InsertBefore("<pages>100</pages>")

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <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>

InsertAfterInsertBeforeInsertElementAfter、および InsertElementBefore メソッドの詳細については、XPathNavigator クラスのリファレンス ドキュメントを参照してください。

子ノードの挿入

XPathNavigator クラスは、子ノードを挿入する次のメソッドを提供します。

これらのメソッドは、XPathNavigator オブジェクトの現在位置にあるノードの一連の子ノードの最後と最初に、子ノードを追加します。

「兄弟ノードの挿入」のメソッドと同様、AppendChild および PrependChild メソッドは、stringXmlReader オブジェクト、または追加する子ノードをパラメーターとして含む XPathNavigator オブジェクトを受け取ります。 両方のメソッドは、子ノードの挿入に使用される XmlWriter オブジェクトも返します。

また、「兄弟ノードの挿入」のメソッドと同様、AppendChildElement および PrependChildElement メソッドは、パラメーターとして指定された名前空間プレフィックス、ローカル名、名前空間 URI、および値を使用して XPathNavigator オブジェクトの現在位置にあるノードの一連の子ノードの最初と最後に 1 つの子ノードを追加します。

次の例では、新しい pages 子要素が book ファイル内の最初の contosoBooks.xml 要素の一連の子要素に追加されます。

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

navigator->AppendChild("<pages>100</pages>");

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

navigator.AppendChild("<pages>100</pages>");

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

navigator.AppendChild("<pages>100</pages>")

Console.WriteLine(navigator.OuterXml)

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <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>

AppendChildPrependChildAppendChildElement、および PrependChildElement メソッドの詳細については、XPathNavigator クラスのリファレンス ドキュメントを参照してください。

属性ノードの挿入

XPathNavigator クラスは、属性ノードを挿入する次のメソッドを提供します。

これらのメソッドは XPathNavigator オブジェクトの現在位置にある要素ノードに属性ノードを挿入します。 CreateAttribute メソッドは、パラメーターとして指定された名前空間プレフィックス、ローカル名、名前空間 URI、および値を使用して XPathNavigator オブジェクトの現在位置にある要素ノードに、属性ノードを作成します。 CreateAttributes メソッドは、属性ノードの挿入に使用される XmlWriter オブジェクトも返します。

次の例では、discount メソッドから返された currency オブジェクトを使用して、新しい price および book 属性が contosoBooks.xml ファイル内の最初の XmlWriter 要素の CreateAttributes 子要素に作成されます。

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");

XmlWriter^ attributes = navigator->CreateAttributes();

attributes->WriteAttributeString("discount", "1.00");
attributes->WriteAttributeString("currency", "USD");
attributes->Close();

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");

XmlWriter attributes = navigator.CreateAttributes();

attributes.WriteAttributeString("discount", "1.00");
attributes.WriteAttributeString("currency", "USD");
attributes.Close();

navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")

Dim attributes As XmlWriter = navigator.CreateAttributes()

attributes.WriteAttributeString("discount", "1.00")
attributes.WriteAttributeString("currency", "USD")
attributes.Close()

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <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>

CreateAttribute および CreateAttributes メソッドの詳細については、XPathNavigator クラスのリファレンス ドキュメントを参照してください。

ノードのコピー

別の XML ドキュメントの内容を使用して XML ドキュメントを作成する場合があります。 XPathNavigator クラスおよび XmlWriter クラスは、既存の XmlDocument オブジェクトまたは XmlReader オブジェクトから XPathNavigator オブジェクトへノードをコピーできます。

AppendChild クラスの PrependChildInsertBeforeInsertAfter、および XPathNavigator メソッドにはすべて、パラメーターとして XPathNavigator オブジェクトまたは XmlReader オブジェクトの受け取りが可能なオーバーロードがあります。

WriteNode クラスの XmlWriter メソッドには、XmlNodeXmlReader、または XPathNavigator オブジェクトの受け取りが可能なオーバーロードがあります。

次の例では、あるドキュメントからすべての book 要素を別のドキュメントにコピーします。

Dim document As XmlDocument = New XmlDocument()  
document.Load("books.xml")  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
navigator.MoveToChild("bookstore", String.Empty)  
  
Dim newBooks As XPathDocument = New XPathDocument("newBooks.xml")  
Dim newBooksNavigator As XPathNavigator = newBooks.CreateNavigator()  
  
Dim nav As XPathNavigator  
For Each nav in newBooksNavigator.SelectDescendants("book", "", false)  
    navigator.AppendChild(nav)  
Next  
  
document.Save("newBooks.xml");  
XmlDocument document = new XmlDocument();  
document.Load("books.xml");  
XPathNavigator navigator = document.CreateNavigator();  
  
navigator.MoveToChild("bookstore", String.Empty);  
  
XPathDocument newBooks = new XPathDocument("newBooks.xml");  
XPathNavigator newBooksNavigator = newBooks.CreateNavigator();  
  
foreach (XPathNavigator nav in newBooksNavigator.SelectDescendants("book", "", false))  
{  
    navigator.AppendChild(nav);  
}  
  
document.Save("newBooks.xml");  

値の挿入

XPathNavigator クラスは、SetValue オブジェクトにノードの値を挿入する SetTypedValue および XmlDocument メソッドを提供しています。

型指定されていない値の挿入

SetValue メソッドは、パラメーターとして渡された型指定されていない string 値を挿入するだけです。このパラメーターは、XPathNavigator オブジェクトの現在位置にあるノードの値です。 値に型は設定されず、スキーマ情報が使用可能な場合でも、ノードの型に対して新しい値が有効どうかを検証せずに挿入されます。

SetValue メソッドを使用して price フィァイル内のすべての contosoBooks.xml 要素を更新する例を次に示します。

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");

for each (XPathNavigator^ nav in navigator->Select("//bk:price", manager))
{
    if(nav->Value == "11.99")
    {
        nav->SetValue("12.99");
    }
}

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

foreach (XPathNavigator nav in navigator.Select("//bk:price", manager))
{
    if (nav.Value == "11.99")
    {
        nav.SetValue("12.99");
    }
}

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

For Each nav As XPathNavigator In navigator.Select("//bk:price", manager)
    If nav.Value = "11.99" Then
        nav.SetValue("12.99")
    End If
Next

Console.WriteLine(navigator.OuterXml)

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <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>

型指定された値の挿入

ノードの型が W3C XML スキーマの単純型の場合、SetTypedValue メソッドによって挿入される新しい値は、値の設定前に、単純型のファセットに対してチェックされます。 新しい値がノードの型に対して無効な場合 (たとえば、型が -1 の要素に、値 xs:positiveInteger を設定するような場合)、例外が返されます。

次の例では、price ファイル内の最初の book 要素の contosoBooks.xml 要素の値を DateTime 値に変更しようとしています。 price 要素の XML スキーマ型は、xs:decimal ファイル内で contosoBooks.xsd として定義されているため、結果は例外になります。

Dim settings As XmlReaderSettings = New XmlReaderSettings()  
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd")  
settings.ValidationType = ValidationType.Schema  
  
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)  
  
Dim document As XmlDocument = New XmlDocument()  
document.Load(reader)  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
navigator.MoveToChild("bookstore", "http://www.contoso.com/books")  
navigator.MoveToChild("book", "http://www.contoso.com/books")  
navigator.MoveToChild("price", "http://www.contoso.com/books")  
  
navigator.SetTypedValue(DateTime.Now)  
XmlReaderSettings settings = new XmlReaderSettings();  
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");  
settings.ValidationType = ValidationType.Schema;  
  
XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);  
  
XmlDocument document = new XmlDocument();  
document.Load(reader);  
XPathNavigator navigator = document.CreateNavigator();  
  
navigator.MoveToChild("bookstore", "http://www.contoso.com/books");  
navigator.MoveToChild("book", "http://www.contoso.com/books");  
navigator.MoveToChild("price", "http://www.contoso.com/books");  
  
navigator.SetTypedValue(DateTime.Now);  

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <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>

また、contosoBooks.xsd ファイルも入力として使用します。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

InnerXml および OuterXml プロパティ

InnerXml クラスの OuterXml および XPathNavigator プロパティは、XPathNavigator オブジェクトの現在位置にある XML マークアップを変更します。

InnerXml プロパティは、与えられた XML XPathNavigator の解析済みの内容を使用して string オブジェクトの現在位置にある子ノードの XML マークアップを変更します。 同様に、OuterXml プロパティは、XPathNavigator オブジェクトの現在位置にある子ノードと現在のノード自体の XML マークアップを変更します。

このトピックで説明したメソッドに加えて、InnerXml プロパティと OuterXml プロパティは、XML ドキュメントからノードに値を挿入するために使用できます。 InnerXml プロパティと OuterXml プロパティを使用してノードと値を挿入する方法の詳細については「XpathNavigator による XML データの変更」のトピックを参照してください。

名前空間と xml:lang の競合

パラメーターとして xml:lang オブジェクトを受け取る InsertBefore クラスの InsertAfterAppendChildPrependChild、および XPathNavigator メソッドを使用して XML データを挿入する場合、名前空間のスコープと XmlReader 宣言に関連する競合が発生する可能性があります。

発生する可能性のある名前空間の競合は次のとおりです。

  • XmlReader オブジェクトのコンテキスト内のスコープに名前空間があり、プレフィックスから名前空間 URI へのマッピングが XPathNavigator オブジェクトのコンテキスト内にない場合、新たに挿入されたノードに関する新しい名前空間宣言が追加されます。

  • XmlReader オブジェクトのコンテキスト内と XPathNavigator オブジェクトのコンテキスト内の両方のスコープに同じ名前空間 URI があるが、両方のコンテキスト内で別のプレフィックスにマップされている場合、XmlReader オブジェクトからのプレフィックスと名前空間 URI を使用して、新たに挿入されたノードに対する新しい名前空間宣言が追加されます。

  • XmlReader オブジェクトのコンテキスト内と XPathNavigator オブジェクトのコンテキスト内の両方のスコープに同じ名前空間プレフィックスがあるが、両方のコンテキスト内で別の名前空間 URI にマップされている場合、新たに挿入されたノードに対する新しい名前空間宣言が追加され、XmlReader オブジェクトからの名前空間 URI を使用してそのプレフィックスを再宣言します。

  • XmlReader オブジェクトのコンテキスト内と XPathNavigator オブジェクトのコンテキスト内の両方で、プレフィックスと名前空間 URI が同じ場合、新たに挿入されたノードに対する新しい名前空間宣言は追加されません。

注意

上記の説明は、プレフィックスとして空の string (たとえば、既定の名前空間宣言) を使用した名前空間宣言にも適用されます。

発生する可能性のある xml:lang の競合は次のとおりです。

  • xml:lang 属性が XmlReader オブジェクトのコンテキスト内のスコープにあるが、XPathNavigator オブジェクトのコンテキスト内にはない場合、xml:lang オブジェクトから受け取った値を持つ XmlReader 属性が、新たに挿入されたノードに対して追加されます。

  • xml:lang 属性が XmlReader オブジェクトのコンテキスト内と XPathNavigator オブジェクトのコンテキスト内の両方のスコープにあるが、それぞれの値が異なる場合、xml:lang オブジェクトから受け取った値を持つ XmlReader 属性が、新たに挿入されたノードに対して追加されます。

  • xml:lang 属性が XmlReader オブジェクトのコンテキスト内と XPathNavigator オブジェクトのコンテキスト内の両方のスコープにあるが、それぞれの値が同じ場合、新たに挿入されたノードに対して新しい xml:lang 属性は追加されません。

  • xml:lang 属性が XPathNavigator オブジェクトのコンテキスト内のスコープにあるが、XmlReader オブジェクトのコンテキスト内にはない場合、新たに挿入されたノードに対して xml:lang 属性は追加されません。

XmlWriter によるノードの挿入

「ノードと値の挿入」に記載されている兄弟ノード、子ノード、および属性ノードの挿入に使用されるメソッドはオーバーロードされます。 InsertAfter クラスの InsertBeforeAppendChildPrependChildCreateAttributes、および XPathNavigator メソッドは、ノードの挿入に使用される XmlWriter オブジェクトを返します。

サポートされていない XmlWriter メソッド

XPath データ モデルとドキュメント オブジェクト モデル (DOM) には相違点があるため、XmlWriter クラスによる XML ドキュメントへの情報の書き込みで使用されるメソッドのすべてが XPathNavigator クラスによってサポートされているわけではありません。

XmlWriter クラスによってサポートされない XPathNavigator クラス メソッドについて次の表で説明します。

メソッド 説明
WriteEntityRef NotSupportedException 例外をスローします。
WriteDocType ルート レベルでは無視され、XML ドキュメント内の他のレベルで呼び出された場合は、NotSupportedException 例外をスローします。
WriteCData 同じ文字または文字列についての WriteString メソッドの呼び出しとして扱われます。
WriteCharEntity 同じ文字または文字列についての WriteString メソッドの呼び出しとして扱われます。
WriteSurrogateCharEntity 同じ文字または文字列についての WriteString メソッドの呼び出しとして扱われます。

XmlWriter クラスに関する詳細については、XmlWriter クラスのリファレンス ドキュメントを参照してください。

複数の XmlWriter オブジェクト

1 つ以上の開いた XPathNavigator オブジェクトを使用して、XML ドキュメントの異なる部分を指す複数の XmlWriter オブジェクトを持つことができます。 複数の XmlWriter オブジェクトは、単一スレッドのシナリオで許可されサポートされます。

以下は、複数の XmlWriter オブジェクト使用時の重要な注意事項です。

  • それぞれの XmlWriter オブジェクトの Close メソッドが呼び出されるときに、XmlWriter オブジェクトによって書き込まれる XML フラグメントが XML ドキュメントに追加されます。 その時点まで、XmlWriter オブジェクトは、まとまっていないフラグメントを書き込んでいます。 XML ドキュメントに対する操作が実行される場合、XmlWriter の呼び出し前に Close オブジェクトによって書き込まれているフラグメントは影響を受けません。

  • 特定の XML サブツリー上に、開いた XmlWriter オブジェクトがあり、そのサブツリーが削除された場合、XmlWriter オブジェクトが引き続きそのサブツリーに追加する可能性があります。 サブツリーは単に削除済みフラグメントになります。

  • XML ドキュメント内の同じ位置で、複数の XmlWriter オブジェクトが開かれた場合、XmlWriter オブジェクトが開かれた順序ではなく、それらが閉じられる順序で XML ドキュメントにそれらが追加されます。

次の例では、XmlDocument オブジェクトを作成し、XPathNavigator オブジェクトを作成した後、XmlWriter メソッドによって返される PrependChild オブジェクトを使用して、books.xml ファイル内に最初の book 構造体を作成します。 この例は、それを book.xml ファイルとして保存します。

Dim document As XmlDocument = New XmlDocument()  
Dim navigator As XPathNavigator = document.CreateNavigator()  
  
Using writer As XmlWriter = navigator.PrependChild()  
  
    writer.WriteStartElement("bookstore")  
    writer.WriteStartElement("book")  
    writer.WriteAttributeString("genre", "autobiography")  
    writer.WriteAttributeString("publicationdate", "1981-03-22")  
    writer.WriteAttributeString("ISBN", "1-861003-11-0")  
    writer.WriteElementString("title", "The Autobiography of Benjamin Franklin")  
    writer.WriteStartElement("author")  
    writer.WriteElementString("first-name", "Benjamin")  
    writer.WriteElementString("last-name", "Franklin")  
    writer.WriteElementString("price", "8.99")  
    writer.WriteEndElement()  
    writer.WriteEndElement()  
    writer.WriteEndElement()  
  
End Using  
  
document.Save("book.xml")  
XmlDocument document = new XmlDocument();  
XPathNavigator navigator = document.CreateNavigator();  
  
using (XmlWriter writer = navigator.PrependChild())  
{  
    writer.WriteStartElement("bookstore");  
    writer.WriteStartElement("book");  
    writer.WriteAttributeString("genre", "autobiography");  
    writer.WriteAttributeString("publicationdate", "1981-03-22");  
    writer.WriteAttributeString("ISBN", "1-861003-11-0");  
    writer.WriteElementString("title", "The Autobiography of Benjamin Franklin");  
    writer.WriteStartElement("author");  
    writer.WriteElementString("first-name", "Benjamin");  
    writer.WriteElementString("last-name", "Franklin");  
    writer.WriteElementString("price", "8.99");  
    writer.WriteEndElement();  
    writer.WriteEndElement();  
    writer.WriteEndElement();  
}  
document.Save("book.xml");  

XML ドキュメントの保存

ここに記載されているメソッドによる XmlDocument オブジェクトに対する変更の保存は、XmlDocument クラスのメソッドを使用して実行されます。 XmlDocument オブジェクトに対する変更の保存に関する詳細については、「ドキュメントの保存と書き込み」を参照してください。

関連項目