次の方法で共有


XPathNavigator を使用して XML データを挿入する

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

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

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

ノードの挿入

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

兄弟ノードの挿入

XPathNavigator クラスには、兄弟ノードを挿入するための次のメソッドが用意されています。

これらのメソッドは、 XPathNavigator オブジェクトが現在配置されているノードの前後に兄弟ノードを挿入します。

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

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

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

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 メソッドは、ノードの子ノードのリストの末尾と先頭に単一の子ノードを挿入します。 XPathNavigator オブジェクトは、現在、名前空間プレフィックス、ローカル名、名前空間 URI、およびパラメーターとして指定された値を使用して配置されています。

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

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 メソッドは、XPathNavigator オブジェクトが現在配置されている要素ノードに、名前空間プレフィックス、ローカル名、名前空間 URI、およびパラメーターとして指定された値を使用して属性ノードを作成します。 CreateAttributes メソッドは、属性ノードの挿入に使用するXmlWriter オブジェクトを返します。

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

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 要素を 1 つのドキュメントから別のドキュメントにコピーします。

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 = 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 プロパティは、XPathNavigator オブジェクトが現在配置されている子ノードの XML マークアップを、指定された XML stringの解析された内容と共に変更します。 同様に、 OuterXml プロパティは、 XPathNavigator オブジェクトが現在配置されている子ノードと現在のノード自体の XML マークアップを変更します。

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

名前空間と xml:lang の競合

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

考えられる名前空間の競合を次に示します。

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

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

  • 同じ名前空間プレフィックスが、 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オブジェクトによって書き込まれた XML フラグメントは、各Close オブジェクトのXmlWriter メソッドが呼び出されたときに XML ドキュメントに追加されます。 その時点まで、 XmlWriter オブジェクトは切断されたフラグメントを書き込みます。 XML ドキュメントに対して操作が実行された場合、XmlWriterが呼び出される前に、Close オブジェクトによって書き込まれるフラグメントは影響を受けません。

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

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

次の例では、XmlDocument オブジェクトを作成し、XPathNavigator オブジェクトを作成し、XmlWriter メソッドから返されたPrependChild オブジェクトを使用して、books.xml ファイル内の最初のブックの構造を作成します。 例は次に、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 オブジェクトに加えられた変更の保存の詳細については、「ドキュメントの保存と書き込み」を参照してください。

こちらも参照ください