XPathNavigator.CreateAttributes 메서드

정의

현재 요소에 새 특성을 만드는 데 사용된 XmlWriter 개체를 반환합니다.

public:
 virtual System::Xml::XmlWriter ^ CreateAttributes();
public virtual System.Xml.XmlWriter CreateAttributes ();
abstract member CreateAttributes : unit -> System.Xml.XmlWriter
override this.CreateAttributes : unit -> System.Xml.XmlWriter
Public Overridable Function CreateAttributes () As XmlWriter

반환

현재 요소에 새 특성을 만드는 데 사용된 XmlWriter 개체입니다.

예외

요소 노드에 XPathNavigator의 위치가 지정되지 않습니다.

XPathNavigator에서는 편집을 지원하지 않습니다.

예제

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

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>  

설명

다음은 메서드를 사용할 때 고려해야 할 중요한 참고 사항입니다 CreateAttributes .

  • XPathNavigator 요소에 배치되면 메서드에서 만든 새 특성이 XPathNavigator 현재 요소의 특성 목록 끝에 배치됩니다.

  • 개체의 메서드가 호출될 때까지 Close 새 특성이 XmlWriter 삽입되지 않습니다.

  • 지정된 네임스페이스 접두사 가 String.Empty 또는 null이면 새 특성의 네임스페이스 URI에 대한 접두사는 현재 scope 네임스페이스에서 가져옵니다. 현재 scope 지정된 네임스페이스 URI에 할당된 네임스페이스 접두사가 없는 경우 네임스페이스 접두사는 자동으로 생성됩니다. 예를 들어 파일의 contosoBooks.xml 기본 네임스페이스(xmlns="http://www.contoso.com/books")에 있는 요소에 새 특성을 만들려면 네임스페이스 접두사 및 네임스페이스 URI 매개 변수 둘 다에 대해 또는 String.Empty 를 지정 null 합니다. http://www.contoso.com/books 네임스페이스 URI 매개 변수로 지정하면 메서드가 CreateAttribute 새 특성에 대한 네임스페이스 접두사를 자동으로 생성합니다.

  • 만든 새 특성이 요소의 네임스페이스 선언과 충돌하는 네임스페이스 노드인 경우, 선택한 네임스페이스 접두사는 동일한 scope 다른 네임스페이스 선언에서 사용되거나 선택한 접두사는 요소의 접두사와 동일하지만 다른 네임스페이스 URI에 바인딩되므로 예외가 throw됩니다.

  • 반환된 개체는 XmlWriter 특성을 만드는 데만 사용할 수 있습니다. 특성을 만들지 않는 다른 XmlWriter 개체 메서드를 호출하면 예외가 발생합니다.

  • 메서드는 CreateAttributesXPathNavigator위치에 영향을 주지 않습니다.

적용 대상