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

一个 XmlReader,该对象定位在新节点的 XML 数据上。

例外

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 并返回 trueXPathNavigator如果 XML 输入Empty位于下一个同级节点或父节点(如果替换的节点没有下一个同级节点,则 返回 ),则 方法完成后ReplaceSelf,不会定位在新XPathNavigator节点上ReplaceSelffalse

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

  • 如果 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 并返回 trueXPathNavigator如果 XML 输入Empty位于下一个同级节点或父节点(如果替换的节点没有下一个同级节点,则 返回 ),则 方法完成后ReplaceSelf,不会定位在新XPathNavigator节点上ReplaceSelffalse

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

  • 如果 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 并返回 trueXPathNavigator如果 XML 输入Empty位于下一个同级节点或父节点(如果替换的节点没有下一个同级节点,则 返回 ),则 方法完成后ReplaceSelf,不会定位在新XPathNavigator节点上ReplaceSelffalse

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

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

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

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

适用于