XPathNavigator를 사용하여 XML 데이터 삽입

업데이트: November 2007

XPathNavigator 클래스는 XML 문서에 형제, 자식 및 특성 노드를 삽입하는 메서드 집합을 제공합니다. 이러한 메서드를 사용하려면 XPathNavigator 개체가 편집 가능한 상태여야 합니다. 즉, CanEdit 속성이 true여야 합니다.

XmlDocument 클래스의 CreateNavigator 메서드에서는 XML 문서를 편집할 수 있는 XPathNavigator 개체를 만듭니다. XPathDocument 클래스에서 만든 XPathNavigator 개체는 읽기 전용이며, 이를 사용하여 XPathDocument 개체에서 만든 XPathNavigator 개체의 메서드를 편집하려는 경우 항상 NotSupportedException이 발생합니다.

편집 가능한 XPathNavigator 개체를 만드는 방법에 대한 자세한 내용은 XPathDocument 및 XmlDocument를 사용하여 XML 데이터 읽기를 참조하십시오.

노드 삽입

XPathNavigator 클래스는 XML 문서에 형제, 자식 및 특성 노드를 삽입하는 메서드를 제공합니다. 이러한 메서드를 사용하여 XPathNavigator 개체의 현재 위치와 관련된 여러 위치에 노드와 특성을 삽입할 수 있습니다. 이 메서드에 대한 자세한 내용은 다음 단원을 참조하십시오.

형제 노드 삽입

XPathNavigator 클래스는 다음과 같은 메서드를 통해 형제 노드를 삽입합니다.

이러한 메서드는 XPathNavigator 개체가 현재 배치되어 있는 노드의 앞뒤에 형제 노드를 삽입합니다.

InsertAfterInsertBefore 메서드가 오버로드되며 string, XmlReader 개체 또는 추가할 형제 노드가 포함된 XPathNavigator 개체를 매개 변수로 허용합니다. 또한 두 메서드는 형제 노드를 삽입하는 데 사용하는 XmlWriter 개체를 반환합니다.

InsertElementAfterInsertElementBefore 메서드는 네임스페이스 접두사, 로컬 이름, 네임스페이스 URI 및 매개 변수로 지정된 값을 사용하여 XPathNavigator 개체가 현재 배치되어 있는 노드 앞뒤에 단일 형제 노드를 삽입합니다.

다음 예제에서는 contosoBooks.xml 파일에서 첫 번째 book 요소의 price 자식 요소 앞에 새 pages 요소를 삽입합니다.

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

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://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", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://www.contoso.com/books");

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

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

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

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

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);

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

<bookstore xmlns="https://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>

InsertAfter, InsertBefore, InsertElementAfterInsertElementBefore 메서드에 대한 자세한 내용은 XPathNavigator 클래스 참조 문서를 참조하십시오.

자식 노드 삽입

XPathNavigator 클래스는 다음과 같은 메서드를 통해 자식 노드를 삽입합니다.

이러한 메서드는 XPathNavigator 개체가 현재 배치되어 있는 노드의 자식 노드 목록 시작과 끝 부분에 자식 노드를 추가합니다.

"형제 노드 삽입" 단원의 메서드와 마찬가지로 AppendChildPrependChild 메서드는 string, XmlReader 개체 또는 추가할 자식 노드가 포함된 XPathNavigator 개체를 매개 변수로 허용합니다. 또한 두 메서드는 자식 노드를 삽입하는 데 사용하는 XmlWriter 개체를 반환합니다.

또한 "형제 노드 삽입" 단원의 메서드와 같이 AppendChildElementPrependChildElement 메서드는 네임스페이스 접두사, 로컬 이름, 네임스페이스 URI 및 매개 변수로 지정된 값을 사용하여 XPathNavigator 개체가 현재 배치되어 있는 노드의 자식 노드 목록 시작과 끝 부분에 단일 자식 노드를 삽입합니다.

다음 예제에서는 contosoBooks.xml 파일에서 첫 번째 book 요소의 자식 요소 목록에 새 pages 자식 요소를 추가합니다.

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

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://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", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");

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

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

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

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

Console::WriteLine(navigator->OuterXml);

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

<bookstore xmlns="https://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>

AppendChild, PrependChild, AppendChildElementPrependChildElement 메서드에 대한 자세한 내용은 XPathNavigator 클래스 참조 문서를 참조하십시오.

특성 노드 삽입

XPathNavigator 클래스는 다음과 같은 메서드를 통해 특성 노드를 삽입합니다.

이러한 메서드는 XPathNavigator 개체가 현재 배치되어 있는 요소 노드에 특성 노드를 삽입합니다. CreateAttribute 메서드는 네임스페이스 접두사, 로컬 이름, 네임스페이스 URI 및 매개 변수로 지정된 값을 사용하여 XPathNavigator 개체가 현재 배치되어 있는 요소 노드에 특성 노드를 만듭니다. CreateAttributes 메서드는 특성 노드를 삽입하는 데 사용하는 XmlWriter 개체를 반환합니다.

다음 예제에서는 CreateAttributes 메서드에서 반환된 XmlWriter 개체를 사용하여 contosoBooks.xml 파일에서 첫 번째 book 요소의 price 자식 요소에 새 discount 및 currency 특성을 만듭니다.

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

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://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)
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://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 = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "https://www.contoso.com/books");
navigator->MoveToChild("book", "https://www.contoso.com/books");
navigator->MoveToChild("price", "https://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);

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

<bookstore xmlns="https://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>

CreateAttributeCreateAttributes 메서드에 대한 자세한 내용은 XPathNavigator 클래스 참조 문서를 참조하십시오.

노드 복사

다른 XML 문서의 내용으로 XML 문서를 채워야 할 경우가 있습니다. XPathNavigator 클래스와 XmlWriter 클래스는 기존 XmlReader 개체 또는 XPathNavigator 개체에서 XmlDocument 개체에 노드를 복사합니다.

XPathNavigator 클래스의 AppendChild, PrependChild, InsertBeforeInsertAfter 메서드는 모두 오버로드되며 XPathNavigator 개체나 XmlReader 개체를 매개 변수로 허용합니다.

XmlWriter 클래스의 WriteNode 메서드는 오버로드되며 XmlNode, XmlReader 또는 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 클래스는 노드 값을 XmlDocument 개체에 삽입하는 SetValueSetTypedValue 메서드를 제공합니다.

형식화되지 않은 값 삽입

SetValue 메서드는 형식화되지 않은 string 값을 간단히 삽입합니다. 이 값은 XPathNavigator 개체가 현재 위치하는 노드의 값인 매개 변수로서 전달됩니다. 이 값은 스키마 정보를 사용할 수 있을 경우 노드 형식에 따라 새 값이 유효한지 여부를 확인하지 않거나 특정한 형식 없이 삽입됩니다.

다음 예제에서는 SetValue 메서드를 사용하여 contosoBooks.xml 파일의 모든 price 요소를 업데이트할 수 있습니다.

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", "https://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)
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "https://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);
XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "https://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);

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

<bookstore xmlns="https://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 메서드에서 삽입한 새 값은 설정되기 전에 단순 형식의 패싯에 대해 검사됩니다. 노드의 형식에 따라 새 값이 유효하지 않은 경우, 예를 들면 xs:positiveInteger 형식의 요소에 -1의 값을 설정하는 경우에는 예외가 발생합니다.

다음 예제에서는 contosoBooks.xml 파일에 있는 첫 번째 book 요소의 price 요소 값을 DateTime 값으로 변경합니다. price 요소의 XML 스키마 형식은 contosoBooks.xsd 파일의 xs:decimal로 정의되므로 예외가 발생합니다.

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("https://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", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://www.contoso.com/books")

navigator.SetTypedValue(DateTime.Now)
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("https://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", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://www.contoso.com/books");

navigator.SetTypedValue(DateTime.Now);

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

<bookstore xmlns="https://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="https://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 속성

XPathNavigator 클래스의 InnerXmlOuterXml 속성은 XPathNavigator 개체가 현재 위치하는 노드의 XML 태그를 변경합니다.

InnerXml 속성은 XPathNavigator 개체가 현재 위치하는 자식 노드의 XML 태그를 주어진 XML string의 구문 분석된 내용과 함께 변경합니다. 마찬가지로, OuterXml 속성은 XPathNavigator 개체가 현재 위치하는 자식 노드의 XML 태그뿐만 아니라 현재 노드 자체도 변경합니다.

이 항목에 설명된 메서드 외에도 InnerXmlOuterXml 속성을 사용하여 XML 문서에 노드와 값을 삽입할 수 있습니다. InnerXmlOuterXml 속성을 사용하여 노드와 값을 삽입하는 방법에 대한 자세한 내용은 XPathNavigator를 사용하여 XML 데이터 수정 항목을 참조하십시오.

네임스페이스와 xml:lang 충돌

XmlReader 개체를 매개 변수로 사용하는 XPathNavigator 클래스의 InsertBefore, InsertAfter, AppendChildPrependChild 메서드를 통해 XML 데이터를 삽입하면 네임스페이스 및 xml:lang 선언의 범위와 관련된 충돌이 발생할 수 있습니다.

다음은 발생할 수 있는 네임스페이스 충돌입니다.

  • XPathNavigator 개체의 컨텍스트에 네임스페이스 URI 접두사 매핑이 없을 경우 XmlReader 개체의 컨텍스트 범위 내에 네임스페이스가 있으면 새로 삽입된 노드에 새 네임스페이스 선언이 추가됩니다.

  • XmlReader 개체의 컨텍스트와 XPathNavigator 개체의 컨텍스트 내에서 같은 네임스페이스 URI가 범위 내에 있지만 두 컨텍스트에서 다른 접두사가 매핑된 경우 새로 삽입된 노드에 새 네임스페이스 선언이 추가되고 XmlReader 개체의 접두사와 네임스페이스 URI가 사용됩니다.

  • XmlReader 개체의 컨텍스트와 XPathNavigator 개체의 컨텍스트 내에서 같은 네임스페이스 접두사가 범위 내에 있지만 두 컨텍스트에 다른 네임스페이스 URI가 매핑된 경우 새로 삽입된 노드에 새 네임스페이스 선언이 추가되며 접두사가 다시 선언되고 XmlReader 개체의 네임스페이스 URI가 사용됩니다.

  • XmlReader 개체의 컨텍스트와 XPathNavigator 개체의 컨텍스트에서 네임스페이스 URI와 접두사가 같을 경우 새로 삽입된 노드에 새 네임스페이스 선언이 추가되지 않습니다.

참고:

위 설명은 기본 네임스페이스 선언과 같이 접두사로 빈 string이 있는 네임스페이스 선언에도 적용됩니다.

다음은 발생할 수 있는 xml:lang 충돌입니다.

  • XmlReader 개체의 컨텍스트 내 범위에 xml:lang 특성이 있지만 XPathNavigator 개체의 컨텍스트에는 없을 경우 XmlReader 개체에서 값을 가져오는 xml:lang 특성이 새로 삽입된 노드에 추가됩니다.

  • XmlReader 개체의 컨텍스트와 XPathNavigator 개체의 컨텍스트 내 범위에 xml:lang 특성이 있지만 각각 값이 다를 경우 XmlReader 개체에서 값을 가져오는 xml:lang 특성이 새로 삽입된 노드에 추가됩니다.

  • XmlReader 개체의 컨텍스트와 XPathNavigator 개체의 컨텍스트 내 범위에 xml:lang 특성이 있고 값이 같을 경우 새로 삽입된 노드에 새 xml:lang 특성이 추가되지 않습니다.

  • XPathNavigator 개체의 컨텍스트 내 범위에 xml:lang 특성이 있지만 XmlReader 개체의 컨텍스트에는 없을 경우 새로 삽입된 노드에 xml:lang 특성이 추가되지 않습니다.

XmlWriter를 사용하여 노드 삽입

"노드 및 값 삽입" 단원에 설명된 형제, 자식 및 특성 노드를 삽입하는 데 사용하는 메서드는 오버로드됩니다. XPathNavigator 클래스의 InsertAfter, InsertBefore, AppendChild, PrependChildCreateAttributes 메서드는 노드를 삽입하는 데 사용하는 XmlWriter 개체를 반환합니다.

지원되지 않는 XmlWriter 메서드

XmlWriter 클래스를 통해 XML 문서에 정보를 작성하는 데 사용되는 메서드 중 일부는 XPath 데이터 모델과 DOM(문서 개체 모델)의 차이로 인해 XPathNavigator 클래스에서 지원되지 않습니다.

다음 표에서는 XPathNavigator 클래스에서 지원하지 않는 XmlWriter 클래스 메서드에 대해 설명합니다.

메서드

설명

WriteEntityRef

NotSupportedException 예외를 throw합니다.

WriteDocType

루트 수준에서 무시되며 XML 문서의 다른 수준에서 호출할 경우 NotSupportedException 예외가 throw됩니다.

WriteCData

해당 문자의 WriteString 메서드에 대한 호출로 처리됩니다.

WriteCharEntity

해당 문자의 WriteString 메서드에 대한 호출로 처리됩니다.

WriteSurrogateCharEntity

해당 문자의 WriteString 메서드에 대한 호출로 처리됩니다.

XmlWriter 클래스에 대한 자세한 내용은 XmlWriter 클래스 참조 문서를 참조하십시오.

여러 XmlWriter 개체

하나 이상의 XmlWriter 개체를 열어 놓은 상태에서 여러 XPathNavigator 개체가 각각 XML 문서의 다른 부분을 가리키도록 할 수 있습니다. 단일 스레드 시나리오에서 여러 XmlWriter 개체가 허용 및 지원됩니다.

다음은 여러 XmlWriter 개체를 사용할 때 고려해야 할 중요 참고 사항입니다.

  • XmlWriter 개체의 Close 메서드를 호출하면 XmlWriter 개체를 사용하여 작성한 XML 조각이 XML 문서에 추가됩니다. 그때까지 XmlWriter 개체는 연결되지 않은 조각을 작성합니다. XML 문서에서 작업을 수행해도 Close를 호출하기 전에 XmlWriter 개체를 사용하여 작성 중인 조각에는 영향을 주지 않습니다.

  • 특정 XML 하위 트리에 XmlWriter 개체가 열려 있을 경우 이 하위 트리를 삭제해도 XmlWriter 개체가 이 하위 트리에 추가됩니다. 하위 트리는 단순히 삭제된 조각이 됩니다.

  • XML 문서의 동일한 지점에 여러 XmlWriter 개체가 동시에 열려 있을 경우 XmlWriter 개체를 연 순서가 아니라 닫은 순서대로 XML 문서에 추가됩니다.

다음 예제에서는 XmlDocument 개체를 만들고 XPathNavigator 개체를 만든 다음 PrependChild 메서드에서 반환된 XmlWriter 개체를 사용하여 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 개체에서 변경된 내용을 저장하는 방법에 대한 자세한 내용은 문서 작성 및 저장을 참조하십시오.

참고 항목

개념

XPath 데이터 모델을 사용하여 XML 데이터 처리

XPathNavigator를 사용하여 XML 데이터 수정

XPathNavigator를 사용하여 XML 데이터 제거

참조

XmlDocument

XPathDocument

XPathNavigator