XPathNavigator.InsertElementAfter(String, String, String, String) Metoda

Definicja

Tworzy nowy element równorzędny po bieżącym węźle przy użyciu prefiksu przestrzeni nazw, nazwy lokalnej i określonego identyfikatora URI przestrzeni nazw z określoną wartością.

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

Parametry

prefix
String

Prefiks przestrzeni nazw nowego elementu podrzędnego (jeśli istnieje).

localName
String

Lokalna nazwa nowego elementu podrzędnego (jeśli istnieje).

namespaceURI
String

Identyfikator URI przestrzeni nazw nowego elementu podrzędnego (jeśli istnieje). Empty i null są równoważne.

value
String

Wartość nowego elementu podrzędnego. Jeśli Empty element zostanie przekazany lub null zostanie przekazany, zostanie utworzony pusty element.

Wyjątki

Pozycja węzła XPathNavigator nie zezwala na wstawianie nowego węzła równorzędnego po bieżącym węźle.

Element XPathNavigator nie obsługuje edycji.

Przykłady

W poniższym przykładzie nowy pages element jest wstawiany po price elemecie podrzędnym pierwszego book elementu w contosoBooks.xml pliku.

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

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

W przykładzie plik jest contosoBooks.xml pobierany jako dane wejściowe.

<?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>  

Uwagi

Prefiks przestrzeni nazw i wartości identyfikatora URI można uzyskać przy użyciu LookupPrefix metody lub LookupNamespace . Na przykład następująca składnia wstawia element równorzędny przy użyciu przestrzeni nazw xmlns:bk="http://www.contoso.com/books"w zakresie :

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

Spowoduje to utworzenie nowego elementu równorzędnego <bk:pages/> .

Poniżej przedstawiono ważne uwagi, które należy wziąć pod uwagę podczas korzystania z InsertElementAfter metody .

  • Jeśli określony prefiks przestrzeni nazw to null lub String.Empty, prefiks identyfikatora URI przestrzeni nazw nowego elementu jest uzyskiwany z bieżących przestrzeni nazw w zakresie. Jeśli nie ma prefiksu przestrzeni nazw przypisanego do określonego identyfikatora URI przestrzeni nazw w bieżącym zakresie, prefiks przestrzeni nazw jest generowany automatycznie.

  • Metoda jest prawidłowa InsertElementAfter tylko wtedy, gdy XPathNavigator element jest umieszczony w węźle elementu, tekstu, przetwarzania instrukcji lub komentarza.

  • Metoda InsertElementAfter nie ma wpływu na położenie XPathNavigatorobiektu .

Dotyczy