XPathNavigator.InsertElementBefore(String, String, String, String) 메서드

정의

지정된 값과 함께 네임스페이스 접두사, 로컬 이름 및 네임스페이스 URI를 사용하여 현재 노드 앞에 새 형제 요소를 만듭니다.

public:
 virtual void InsertElementBefore(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void InsertElementBefore (string prefix, string localName, string namespaceURI, string value);
public virtual void InsertElementBefore (string? prefix, string localName, string? namespaceURI, string? value);
abstract member InsertElementBefore : string * string * string * string -> unit
override this.InsertElementBefore : string * string * string * string -> unit
Public Overridable Sub InsertElementBefore (prefix As String, localName As String, namespaceURI As String, value As String)

매개 변수

prefix
String

새 자식 요소의 네임스페이스 접두사입니다(있는 경우).

localName
String

새 자식 요소의 로컬 이름입니다(있는 경우).

namespaceURI
String

새 자식 요소의 네임스페이스 URI입니다(있는 경우). Emptynull는 동일합니다.

value
String

새 자식 요소의 값입니다. Empty 또는 null을 전달하면 빈 요소가 생성됩니다.

예외

XPathNavigator 위치에서는 현재 노드 앞에 새로운 형제 노드를 삽입할 수 없습니다.

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

예제

다음 예제에서는 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->InsertElementBefore(navigator->Prefix, "pages", navigator->LookupNamespace(navigator->Prefix), "100");

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.InsertElementBefore(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

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.InsertElementBefore(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100")

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>  

설명

네임스페이스 접두사 및 URI 값은 또는 LookupNamespace 메서드를 LookupPrefix 사용하여 가져올 수 있습니다. 예를 들어 다음 구문은 scope 네임스페이스 를 사용하여 형제 요소를 삽입합니다xmlns:bk="http://www.contoso.com/books".

navigator.InsertElementBefore(navigator.Prefix, "pages", LookupNamespaceURI(navigator.Prefix), String.Empty)  

그러면 새 형제 <bk:pages/> 요소가 만들어집니다.

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

  • 지정된 네임스페이스 접두사 가 null 또는 String.Empty이면 새 요소의 네임스페이스 URI에 대한 접두사는 현재 scope 네임스페이스에서 가져옵니다. 현재 scope 지정된 네임스페이스 URI에 할당된 네임스페이스 접두사가 없는 경우 네임스페이스 접두사는 자동으로 생성됩니다.

  • 메서드는 InsertElementBefore 가 요소, 텍스트, 처리 명령 또는 주석 노드에 배치된 경우에만 XPathNavigator 유효합니다.

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

적용 대상