語言

XPathNavigator.ReplaceSelf 方法

定義

替換為指定內容的當前節點。

多載

名稱 Description
ReplaceSelf(XmlReader)

將當前節點替換為指定物件的內容 XmlReader 。

ReplaceSelf(XPathNavigator)

將當前節點替換為指定物件的內容 XPathNavigator 。

ReplaceSelf(String)

以指定字串內容替換當前節點。

ReplaceSelf(XmlReader)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
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

一個 XmlReader 放置在新節點 XML 資料上的物件。

例外狀況

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

XmlReader物件參數為 null。

該節點 XPathNavigator 未被定位於元素、文字、處理指令或註解節點。

它 XPathNavigator 不支援編輯。

物件參數的 XmlReader XML 內容並非良好格式。

範例

以下範例 price 中,檔案中的 contosoBooks.xml 元素被一個新 pages 元素取代。

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 輸入為 ReplaceSelf 時,方法完成Empty後 不會被放置在新節點上,若 XPathNavigator 沒有被取代節點的下一個兄弟節點,則 會被放置在下一個兄弟節點或父節點上,且ReplaceSelf返回 false。

  • 此 ReplaceSelf 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。

  • 若 XML 資料字串非良構,則會拋出例外,此方法的結果等同於對當前節點的呼叫 DeleteSelf 。

  • 若 XML 字串包含多個節點,則所有節點都會被加入,並將 XPathNavigator 置於該串節點的第一個節點上。

  • 此 ReplaceSelf 方法與該 DeleteSelf 方法不等價。

適用於

ReplaceSelf(XPathNavigator)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
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 不支援編輯。

物件參數的 XPathNavigator XML 內容並非良好格式。

範例

以下範例 price 中,檔案中的 contosoBooks.xml 元素被一個新 pages 元素取代。

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 輸入為 ReplaceSelf 時,方法完成Empty後 不會被放置在新節點上,若 XPathNavigator 沒有被取代節點的下一個兄弟節點,則 會被放置在下一個兄弟節點或父節點上,且ReplaceSelf返回 false。

  • 此 ReplaceSelf 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。

  • 若 XML 資料字串非良構,則會拋出例外,此方法的結果等同於對當前節點的呼叫 DeleteSelf 。

  • 若 XML 字串包含多個節點,則所有節點都會被加入,並將 XPathNavigator 置於該串節點的第一個節點上。

  • 此 ReplaceSelf 方法與該 DeleteSelf 方法不等價。

適用於

ReplaceSelf(String)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
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 未被定位於元素、文字、處理指令或註解節點。

它 XPathNavigator 不支援編輯。

XML 字串參數並非良好構想。

範例

以下範例 price 中,檔案中的 contosoBooks.xml 元素被一個新 pages 元素取代。

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 輸入為 ReplaceSelf 時,方法完成Empty後 不會被放置在新節點上,若 XPathNavigator 沒有被取代節點的下一個兄弟節點,則 會被放置在下一個兄弟節點或父節點上,且ReplaceSelf返回 false。

  • 此 ReplaceSelf 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。

  • 若 XML 資料字串非良構,則會拋出例外,此方法的結果等同於對當前節點的呼叫 DeleteSelf 。

  • 若 XML 字串包含多個節點,則所有節點都會被加入,並將 XPathNavigator 置於該串節點的第一個節點上。

  • 此 ReplaceSelf 方法與該 DeleteSelf 方法不等價。

適用於