이 클래스는 XPathNavigator XML 문서에 형제, 자식 및 특성 노드를 삽입하는 데 사용되는 메서드 집합을 제공합니다. 이러한 메서드를 사용하려면 XPathNavigator 개체를 편집할 수 있어야 합니다. 즉, CanEdit 속성이 true
일 수 있어야 합니다.
XPathNavigatorXML 문서를 편집할 수 있는 개체는 클래스의 CreateNavigator 메서드에 XmlDocument 의해 만들어집니다. XPathNavigator클래스에서 생성된 XPathDocument 개체는 읽기 전용이며, XPathNavigator 개체에서 생성된 XPathDocument 개체의 편집 메서드를 사용하려고 하면 NotSupportedException가 발생합니다.
편집 가능한 XPathNavigator 개체를 만드는 방법에 대한 자세한 내용은 XPathDocument 및 XmlDocument를 사용하여 XML 데이터 읽기를 참조하세요.
노드 삽입
이 클래스는 XPathNavigator XML 문서에 형제, 자식 및 특성 노드를 삽입하는 메서드를 제공합니다. 이러한 메서드를 사용하면 개체의 XPathNavigator 현재 위치와 관련하여 다른 위치에 노드 및 특성을 삽입할 수 있으며 다음 섹션에 설명되어 있습니다.
형제 노드 삽입
클래스는 XPathNavigator 형제 노드를 삽입하는 다음 메서드를 제공합니다.
이러한 메서드는 개체가 현재 배치된 노드 앞과 뒤에 형제 노드 XPathNavigator 를 삽입합니다.
InsertAfter 및 InsertBefore 메서드는 오버로드되어 string
, XmlReader 개체 또는 형제 노드를 추가할 XPathNavigator 개체를 매개변수로 허용합니다. 또한 두 메서드는 형제 노드를 XmlWriter 삽입하는 데 사용되는 개체를 반환합니다.
InsertElementAfter 메서드 및 InsertElementBefore 메서드는 개체가 현재 위치한 노드 앞과 뒤에 네임스페이스 접두사, 로컬 이름, 네임스페이스 URI 및 매개 변수로 지정된 값을 사용하여 단일 형제 노드를 삽입합니다.
다음 예제에서는 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>
기타 InsertAfter, InsertBefore, InsertElementAfter 및 InsertElementBefore 메서드에 대한 자세한 내용은 XPathNavigator 클래스 참조 설명서를 참조하세요.
자식 노드 삽입
클래스는 XPathNavigator 자식 노드를 삽입하는 다음 메서드를 제공합니다.
이러한 메서드는 XPathNavigator 객체가 현재 위치한 노드의 자식 노드 목록의 시작과 끝에 자식 노드를 각각 추가합니다.
"형제 노드 삽입" 섹션의 메서드와 마찬가지로, AppendChild 및 PrependChild 메서드는 매개변수로 추가할 자식 노드를 포함하는 string
, XmlReader 객체, 또는 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>
기타 AppendChild, PrependChild, AppendChildElement 및 PrependChildElement 메서드에 대한 자세한 내용은 XPathNavigator 클래스 참조 설명서를 참조하세요.
특성 노드 삽입
이 클래스는 XPathNavigator 특성 노드를 삽입하는 다음 메서드를 제공합니다.
이러한 메서드는 개체가 현재 배치된 요소 노드에 특성 노드 XPathNavigator 를 삽입합니다. 메서드는 CreateAttribute 현재 네임스페이스 접두사, 로컬 이름, 네임스페이스 URI 및 매개 변수로 지정된 값을 사용하여 개체가 배치된 요소 노드 XPathNavigator 에 특성 노드를 만듭니다. 이 메서드는 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, PrependChild, InsertBefore 및 InsertAfter 메서드는 모두 XPathNavigator 개체 또는 XPathNavigator 개체를 매개 변수로 허용할 수 있는 오버로드를 XmlReader 클래스에 가지고 있습니다.
WriteNode 클래스의 XmlWriter 메서드에는 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 클래스는 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 값으로 변경하려고 시도합니다. XML 스키마 형식이 price
파일에서 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 개체가 현재 위치한 노드의 XML 마크업을 변경합니다.
현재 InnerXml 개체가 위치한 자식 노드의 XML 마크업을 지정된 XPathNavigator XML의 구문 분석된 내용으로 변경합니다 string
. 마찬가지로 이 속성은 OuterXml 현재 노드 XPathNavigator 자체뿐만 아니라 개체가 현재 배치된 자식 노드의 XML 태그를 변경합니다.
이 주제에 설명된 메서드 외에도, InnerXml 및 OuterXml 속성을 사용하여 XML 문서에 노드와 값을 삽입할 수 있습니다. 및 속성을 사용하여 InnerXmlOuterXml 노드 및 값을 삽입하는 방법에 대한 자세한 내용은 XPathNavigator 항목을 사용하여 XML 데이터 수정 항목을 참조하세요.
네임스페이스 및 xml:lang 충돌
네임스페이스와 xml:lang
선언과 관련된 특정 충돌은 InsertBefore 객체를 매개변수로 사용하는 InsertAfter 클래스의 AppendChildPrependChildXPathNavigatorXmlReader 메서드를 사용하여 XML 데이터를 삽입할 때 발생할 수 있습니다.
다음은 가능한 네임스페이스 충돌입니다.
네임스페이스 URI 매핑의 접두사가 개체의 컨텍스트에 없는 XmlReader 개체의 컨텍스트 내에 XPathNavigator 범위 내 네임스페이스가 있는 경우 새로 삽입된 노드에 새 네임스페이스 선언이 추가됩니다.
동일한 네임스페이스 URI가 XmlReader 및 XPathNavigator 객체의 컨텍스트 범위 내에 포함되어 있으나 두 컨텍스트에서 서로 다른 접두사가 매핑된 경우, 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, InsertBefore, AppendChild, PrependChild 및 CreateAttributes 메서드는 클래스의 XPathNavigator에서 노드를 삽입하는 데 사용되는 XmlWriter 객체를 반환합니다.
지원되지 않는 XmlWriter 메서드
XPath 데이터 모델과 문서 객체 모델(DOM)의 차이로 인해 XmlWriter 클래스를 사용하여 XML 문서에 정보를 쓰는 데 사용되는 모든 메서드가 XPathNavigator 클래스에서 지원되지 않습니다.
다음 표에서는 XmlWriter 클래스에서 지원하지 않는 XPathNavigator 클래스 메서드에 대해 설명합니다.
메서드 | 설명 |
---|---|
WriteEntityRef | NotSupportedException 예외를 던집니다. |
WriteDocType | 루트 수준에서는 무시되고, XML 문서의 다른 수준에서 호출되면 NotSupportedException 예외를 던집니다. |
WriteCData | WriteString 메서드에 대한 호출로 해당 문자 또는 문자열이 처리됩니다. |
WriteCharEntity | WriteString 메서드에 대한 호출로 해당 문자 또는 문자열이 처리됩니다. |
WriteSurrogateCharEntity | WriteString 메서드에 대한 호출로 해당 문자 또는 문자열이 처리됩니다. |
XmlWriter 클래스에 대한 자세한 내용은 XmlWriter 클래스 참조 설명서를 참조하세요.
여러 XmlWriter 개체
하나 이상의 XPathNavigator 개체가 열린 상태에서 XmlWriter 개체 여러 개가 다양한 XML 문서의 부분을 가리킬 수 있습니다. 단일 스레드 시나리오에서는 여러 XmlWriter 개체가 허용되고 지원됩니다.
다음은 여러 XmlWriter 개체를 사용할 때 고려해야 할 중요한 참고 사항입니다.
각 XmlWriter 개체의 메서드가 호출되면 개체가 작성한 Close XML 조각이 XML 문서에 XmlWriter 추가됩니다. 이 시점까지 개체는 XmlWriter 연결이 끊긴 조각을 작성합니다. XML 문서에서 작업을 수행하는 경우, XmlWriter가 호출되기 전이라면 Close 객체가 작성 중인 조각은 영향을 받지 않습니다.
특정 XML 하위 트리에 열려 XmlWriter 있는 개체가 있고 해당 하위 트리가 삭제된 경우에도 개체가 XmlWriter 하위 트리에 추가될 수 있습니다. 하위 트리는 단순히 삭제된 조각이 됩니다.
XML 문서의 같은 지점에서 여러 XmlWriter 개체를 열면 개체가 열린 순서가 아니라 개체가 닫힌 순서 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 저장하는 방법에 대한 자세한 내용은 문서 저장 및 쓰기를 참조하세요.
참고하십시오
.NET