XPathNavigator.DeleteSelf 方法

定義

刪除目前的節點和它的子節點。

public:
 virtual void DeleteSelf();
public virtual void DeleteSelf ();
abstract member DeleteSelf : unit -> unit
override this.DeleteSelf : unit -> unit
Public Overridable Sub DeleteSelf ()

例外狀況

XPathNavigator 會放置在無法刪除的節點上,例如根節點或命名空間節點。

範例

在下列範例中,會 price 使用 DeleteSelf 方法刪除檔案第一個專案 bookcontosoBooks.xml 元素。 刪除 XPathNavigator 項目之後,price 物件的位置在父 book 項目上。

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

Console::WriteLine("Position after delete: {0}", navigator->Name);
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.DeleteSelf();

Console.WriteLine("Position after delete: {0}", navigator.Name);
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.DeleteSelf()

Console.WriteLine("Position after delete: {0}", navigator.Name)
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>  

備註

  • 在成功刪除時,位於 XPathNavigator 已刪除節點的父節點。

  • 如果已刪除的節點是簡單型別專案內容的文位元組點,則專案會遺失其類型資訊。 這表示在 專案上 XmlType 時,、 ValueTypeTypedValue 屬性分別具有 和 值 StringnullEmpty

  • 刪除的節點在刪除之前,仍可供 XPathNavigator 放置於這些節點的物件存取。 不過,嘗試離開已刪除子樹狀結構的方法會失敗。 例如,當 位於最上層已刪除的節點上方時 XPathNavigatorMoveToParentMoveToPrevious 方法一律會失敗。 這是因為已刪除的節點不再連線到 XML 檔。 同樣地, MoveToNext 如果 XPathNavigator 位於最下層刪除的節點上,方法一律會失敗。

適用於