XPathNavigator.InsertElementAfter(String, String, String, String) 方法

定义

使用通过指定值指定的命名空间前缀、本地名称和命名空间 URI 在当前节点之后创建一个新的同级元素。Creates a new sibling element after the current node using the namespace prefix, local name and namespace URI specified, with the value specified.

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);
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)

参数

prefix
String

新的子元素的命名空间前缀(如果有)。The namespace prefix of the new child element (if any).

localName
String

新的子元素的本地名称(如果有)。The local name of the new child element (if any).

namespaceURI
String

新的子元素的命名空间 URI(如果有)。The namespace URI of the new child element (if any). Emptynull 等效。Empty and null are equivalent.

value
String

新的子元素的值。The value of the new child element. 如果传递 Emptynull,将创建一个空元素。If Empty or null are passed, an empty element is created.

例外

XPathNavigator 的位置不允许在当前节点之后插入新的同级节点。The position of the XPathNavigator does not allow a new sibling node to be inserted after the current node.

XPathNavigator 不支持编辑。The XPathNavigator does not support editing.

示例

在下面的示例中,在 pages price 文件中第一个元素的子元素之后插入一个新元素 book contosoBooks.xmlIn the following example a new pages element is inserted after the price child element of the first book element in the contosoBooks.xml file.

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)

该示例使用 contosoBooks.xml 文件作为输入。The example takes the contosoBooks.xml file as an input.

<?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 LookupPrefixLookupNamespaceNamespace prefix and URI values can be obtained using the LookupPrefix or LookupNamespace method. 例如,以下语法使用范围内命名空间插入同级元素 xmlns:bk="http://www.contoso.com/books"For example, the following syntax inserts a sibling element by using the in-scope namespace xmlns:bk="http://www.contoso.com/books":

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

这会创建新的同级 <bk:pages/> 元素。This creates the new sibling <bk:pages/> element.

以下是使用方法时要考虑的重要注意事项 InsertElementAfterThe following are important notes to consider when using the InsertElementAfter method.

  • 如果指定的命名空间前缀为 nullString.Empty ,则将从作用域内的当前命名空间中获取新元素的命名空间 URI 的前缀。If the namespace prefix specified is null or String.Empty, then the prefix for the namespace URI of the new element is obtained from the current namespaces in-scope. 如果当前范围内未给指定的命名空间 URI 分配命名空间前缀,则会自动生成一个命名空间前缀。If there is no namespace prefix assigned to the specified namespace URI at the current scope, then a namespace prefix is automatically generated.

  • InsertElementAfter仅当位于 XPathNavigator 元素、文本、处理指令或注释节点上时,此方法才有效。The InsertElementAfter method is valid only when the XPathNavigator is positioned on an element, text, processing instruction, or comment node.

  • InsertElementAfter方法不影响的位置 XPathNavigatorThe InsertElementAfter method does not affect the position of the XPathNavigator.

适用于