XPathNavigator.ReplaceSelf 方法

定義

將目前的節點取代為指定的內容。

多載

ReplaceSelf(XmlReader)

將目前的節點取代為指定 XmlReader 物件的內容。

ReplaceSelf(XPathNavigator)

將目前的節點取代為指定 XPathNavigator 物件的內容。

ReplaceSelf(String)

將目前的節點取代為指定字串的內容。

ReplaceSelf(XmlReader)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

將目前的節點取代為指定 XmlReader 物件的內容。

public:
 virtual void ReplaceSelf(System::Xml::XmlReader ^ newNode);
public virtual void ReplaceSelf (System.Xml.XmlReader newNode);
abstract member ReplaceSelf : System.Xml.XmlReader -> unit
override this.ReplaceSelf : System.Xml.XmlReader -> unit
Public Overridable Sub ReplaceSelf (newNode As XmlReader)

參數

newNode
XmlReader

位於新節點之 XML 資料上的 XmlReader 物件。

例外狀況

XmlReader 物件處於錯誤狀態或已關閉。

XmlReader 物件參數為 null

XPathNavigator 不在項目、文字、處理指示或註解節點上。

XmlReader 物件參數的 XML 內容語式不正確。

範例

在下列範例中, price 檔案中的 contosoBooks.xml 專案會由新的 pages 元素取代。

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

XmlReader^ pages = XmlReader::Create(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator->ReplaceSelf(pages);

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

XmlReader pages = XmlReader.Create(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator.ReplaceSelf(pages);

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

Dim pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))

navigator.ReplaceSelf(pages)

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 方法完成 ReplaceSelf 時的新節點上,並傳 true 回 。 XPathNavigator當 XML 輸入位於下一個同層級節點或父節點,如果取代的節點沒有下一個同層級節點,且 傳 ReplaceSelffalse 時,XML 輸入 EmptyXPathNavigator 完成 ReplaceSelf 之後,就不會位於方法的新節點上。

  • 只有在 位於專案、文字、處理指示或批註節點上時 XPathNavigator ,方法 ReplaceSelf 才有效。

  • 如果 XML 資料字串的格式不正確,則會擲回例外狀況,而且這個方法的結果相當於在目前的節點上呼叫 DeleteSelf

  • 如果 XML 字串包含多個節點,則會新增所有節點,且 XPathNavigator 位於一系列節點的第一個節點上。

  • 方法 ReplaceSelf 不等於 DeleteSelf 方法。

適用於

ReplaceSelf(XPathNavigator)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

將目前的節點取代為指定 XPathNavigator 物件的內容。

public:
 virtual void ReplaceSelf(System::Xml::XPath::XPathNavigator ^ newNode);
public virtual void ReplaceSelf (System.Xml.XPath.XPathNavigator newNode);
abstract member ReplaceSelf : System.Xml.XPath.XPathNavigator -> unit
override this.ReplaceSelf : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub ReplaceSelf (newNode As XPathNavigator)

參數

newNode
XPathNavigator

位於新節點上的 XPathNavigator 物件。

例外狀況

XPathNavigator 物件參數為 null

XPathNavigator 不在項目、文字、處理指示或註解節點上。

XPathNavigator 物件參數的 XML 內容語式不正確。

範例

在下列範例中, price 檔案中的 contosoBooks.xml 專案會由新的 pages 元素取代。

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

XmlDocument^ childNodes = gcnew XmlDocument();
childNodes->Load(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator^ childNodesNavigator = childNodes->CreateNavigator();

navigator->ReplaceSelf(childNodesNavigator);

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

XmlDocument childNodes = new XmlDocument();
childNodes.Load(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator childNodesNavigator = childNodes.CreateNavigator();

navigator.ReplaceSelf(childNodesNavigator);

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

Dim childNodes As XmlDocument = New XmlDocument()
childNodes.Load(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
Dim childNodesNavigator As XPathNavigator = childNodes.CreateNavigator()

navigator.ReplaceSelf(childNodesNavigator)

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 方法完成 ReplaceSelf 時的新節點上,並傳 true 回 。 XPathNavigator當 XML 輸入位於下一個同層級節點或父節點,如果取代的節點沒有下一個同層級節點,且 傳 ReplaceSelffalse 時,XML 輸入 EmptyXPathNavigator 完成 ReplaceSelf 之後,就不會位於方法的新節點上。

  • 只有在 位於專案、文字、處理指示或批註節點上時 XPathNavigator ,方法 ReplaceSelf 才有效。

  • 如果 XML 資料字串的格式不正確,則會擲回例外狀況,而且這個方法的結果相當於在目前的節點上呼叫 DeleteSelf

  • 如果 XML 字串包含多個節點,則會新增所有節點,且 XPathNavigator 位於一系列節點的第一個節點上。

  • 方法 ReplaceSelf 不等於 DeleteSelf 方法。

適用於

ReplaceSelf(String)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

將目前的節點取代為指定字串的內容。

public:
 virtual void ReplaceSelf(System::String ^ newNode);
public virtual void ReplaceSelf (string newNode);
abstract member ReplaceSelf : string -> unit
override this.ReplaceSelf : string -> unit
Public Overridable Sub ReplaceSelf (newNode As String)

參數

newNode
String

新的節點之 XML 資料字串。

例外狀況

XML 字串參數為 null

XPathNavigator 不在項目、文字、處理指示或註解節點上。

XML 字串參數的語式不正確。

範例

在下列範例中, price 檔案中的 contosoBooks.xml 專案會由新的 pages 元素取代。

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->ReplaceSelf("<pages>100</pages>");

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.ReplaceSelf("<pages>100</pages>");

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.ReplaceSelf("<pages>100</pages>")

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 方法完成 ReplaceSelf 時的新節點上,並傳 true 回 。 XPathNavigator當 XML 輸入位於下一個同層級節點或父節點,如果取代的節點沒有下一個同層級節點,且 傳 ReplaceSelffalse 時,XML 輸入 EmptyXPathNavigator 完成 ReplaceSelf 之後,就不會位於方法的新節點上。

  • 只有在 位於專案、文字、處理指示或批註節點上時 XPathNavigator ,方法 ReplaceSelf 才有效。

  • 如果 XML 資料字串的格式不正確,則會擲回例外狀況,而且這個方法的結果相當於在目前的節點上呼叫 DeleteSelf

  • 如果 XML 字串包含多個節點,則會新增所有節點,且 XPathNavigator 位於一系列節點的第一個節點上。

  • 方法 ReplaceSelf 不等於 DeleteSelf 方法。

適用於