语言

XPathNavigator.ReplaceSelf 方法

定义

将当前节点替换为指定的内容。

重载

名称 说明
ReplaceSelf(XmlReader)

将当前节点替换为指定的对象的内容 XmlReader 。

ReplaceSelf(XPathNavigator)

将当前节点替换为指定的对象的内容 XPathNavigator 。

ReplaceSelf(String)

将当前节点替换为指定的字符串的内容。

ReplaceSelf(XmlReader)

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

XmlReader位于新节点的 XML 数据上的对象。

例外

对象 XmlReader 处于错误状态或已关闭。

对象 XmlReader 参数为 null.

该 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 输入ReplaceSelfEmpty位于下一个同级节点或父节点(如果没有替换的节点和XPathNavigator返回ReplaceSelf的下一个同级节点)时,该方法完成false后,该节点不会定位在新节点上。

  • ReplaceSelf该方法仅在定位在元素、文本、处理指令或注释节点上时才XPathNavigator有效。

  • 如果 XML 数据字符串的格式不如引发异常,并且此方法的结果等效于在当前节点上调用 DeleteSelf 。

  • 如果 XML 字符串包含多个节点,则会添加所有节点,并将 XPathNavigator 该节点定位在一系列节点的第一个节点上。

  • 该方法 ReplaceSelf 不等效于 DeleteSelf 该方法。

适用于

ReplaceSelf(XPathNavigator)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
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 = 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 输入ReplaceSelfEmpty位于下一个同级节点或父节点(如果没有替换的节点和XPathNavigator返回ReplaceSelf的下一个同级节点)时,该方法完成false后,该节点不会定位在新节点上。

  • ReplaceSelf该方法仅在定位在元素、文本、处理指令或注释节点上时才XPathNavigator有效。

  • 如果 XML 数据字符串的格式不如引发异常,并且此方法的结果等效于在当前节点上调用 DeleteSelf 。

  • 如果 XML 字符串包含多个节点,则会添加所有节点,并将 XPathNavigator 该节点定位在一系列节点的第一个节点上。

  • 该方法 ReplaceSelf 不等效于 DeleteSelf 该方法。

适用于

ReplaceSelf(String)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
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 = 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 输入ReplaceSelfEmpty位于下一个同级节点或父节点(如果没有替换的节点和XPathNavigator返回ReplaceSelf的下一个同级节点)时,该方法完成false后,该节点不会定位在新节点上。

  • ReplaceSelf该方法仅在定位在元素、文本、处理指令或注释节点上时才XPathNavigator有效。

  • 如果 XML 数据字符串的格式不如引发异常,并且此方法的结果等效于在当前节点上调用 DeleteSelf 。

  • 如果 XML 字符串包含多个节点,则会添加所有节点,并将 XPathNavigator 该节点定位在一系列节点的第一个节点上。

  • 该方法 ReplaceSelf 不等效于 DeleteSelf 该方法。

适用于