zoukankan      html  css  js  c++  java
  • XmlNode.ReplaceChild 方法(http://kingcms.cn/XML/index.htm)

    XmlNode.ReplaceChild 方法
    用 newChild 节点替换子节点 oldChild。

    [Visual Basic]
    Public Overridable Function ReplaceChild( _
       ByVal newChild As XmlNode, _
       ByVal oldChild As XmlNode _
    ) As XmlNode
    [C#]
    public virtual XmlNode ReplaceChild(
       XmlNode newChild,
       XmlNode oldChild
    );
    [C++]
    public: virtual XmlNode* ReplaceChild(
       XmlNode* newChild,
       XmlNode* oldChild
    );
    [JScript]
    public function ReplaceChild(
       newChild : XmlNode,
       oldChild : XmlNode
    ) : XmlNode;
    参数
    newChild
    要放入子列表的新节点。
    oldChild
    列表中正在被替换的节点。
    返回值
    被替换的节点。

    异常
    异常类型 条件
    InvalidOperationException 此节点的类型不允许 newChild 节点类型的子节点。
    newChild 是此节点的上级节点。
     
    ArgumentException newChild 是从不同于创建此节点的文档创建的。
    该节点是只读的。

    oldChild 不是此节点的子级。
     

    备注
    如果 newChild 已经在树中,则先将其移除。

    如果 newChild 是从另一个文档中创建的,可使用 XmlDocument.ImportNode 将节点导入到当前文档。然后可将导入的节点传递给 ReplaceChild 方法。

    对继承者的说明:  当在派生类中重写 ReplaceChild 时,若要正确激发事件,必须调用基类的 ReplaceChild 方法。

    示例
    [Visual Basic, C#] 下面的示例替换 XML 中的标题元素。

    [Visual Basic]
    Imports System
    Imports System.IO
    Imports System.Xml

    Public Class Sample
       
        Public Shared Sub Main()
           
            Dim doc As New XmlDocument()
            doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                        "<title>Pride And Prejudice</title>" & _
                        "</book>")
           
            Dim root As XmlNode = doc.DocumentElement
           
            'Create a new title element.
            Dim elem As XmlElement = doc.CreateElement("title")
            elem.InnerText = "The Handmaid's Tale"
           
            'Replace the title element.
            root.ReplaceChild(elem, root.FirstChild)
           
            Console.WriteLine("Display the modified XML...")
            doc.Save(Console.Out)
        End Sub 'Main
    End Class 'Sample

    [C#]
    using System;
    using System.IO;
    using System.Xml;

    public class Sample {

      public static void Main() {

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "</book>");

        XmlNode root = doc.DocumentElement;

        //Create a new title element.
        XmlElement elem = doc.CreateElement("title");
        elem.InnerText="The Handmaid's Tale";

        //Replace the title element.
        root.ReplaceChild(elem, root.FirstChild);

        Console.WriteLine("Display the modified XML...");
        doc.Save(Console.Out);

      }
    }

    public void ReplaceNodeTest()
            
    {
                XmlDocument doc 
    = new XmlDocument();
                doc.LoadXml(
    "<book genre='novel' ISBN='1-861001-57-5'>" +
                    
    "<title>Pride And Prejudice</title> " +
                    
    "<t3><t2>2</t2><t1>t2 is removed</t1></t3>" +
                    
    "</book>");
                XmlNode root 
    = doc.DocumentElement;
                
    //Create a new title element.
                XmlElement elem = doc.CreateElement("title");
                elem.InnerText
    ="The Handmaid's Tale";
                XmlNode t2 
    = doc.DocumentElement.SelectSingleNode("t3/t2");
                
    //Replace the title element.
                root.ReplaceChild(t2, root.FirstChild);            
                
    this.LabelTest.Text = Page.Server.HtmlEncode(doc.OuterXml);

            }
  • 相关阅读:
    Excel中substitute替换函数的使用方法
    如何在Excel中提取小数点后面的数字?
    提升单元测试体验的利器--Mockito使用总结
    SpringMVC项目读取不到外部CSS文件的解决办法及总结
    java8 Lambda表达式的新手上车指南(1)--基础语法和函数式接口
    Spring-data-redis操作redis知识总结
    优雅高效的MyBatis-Plus工具快速入门使用
    Thrift入门初探(2)--thrift基础知识详解
    Thrift入门初探--thrift安装及java入门实例
    spring事件驱动模型--观察者模式在spring中的应用
  • 原文地址:https://www.cnblogs.com/snowball/p/462827.html
Copyright © 2011-2022 走看看