如题!
如果将一个XML文件的XMLNODE 添加到另一个XML文件中

解决方案 »

  1.   

    use XmlDocument's ImportNode method to import the node to the second document, and then append it to some node
      

  2.   

    using System;
    using System.IO;
    using System.Xml;public class Class1
    {
    public static void Main()
    {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<bookstore>" +
    "<book genre='novel' ISBN='1-861001-57-5'>" +
    "<title>Pride And Prejudice</title>" +
    "</book>" +
    "</bookstore>"); //Create another XmlDocument which holds a list of books.
    XmlDocument doc2 = new XmlDocument();
    doc2.Load("books.xml"); //Import the last book node from doc2 into the original document.
    XmlNode newBook = doc2.ImportNode(doc.DocumentElement.LastChild, true);
    doc2.DocumentElement.AppendChild(newBook); 
    doc2.Save("books.xml");
        
    }
    }
    该示例使用文件 books.xml 
    <?xml version='1.0'?>
    <!-- This file represents a fragment of a book store inventory database -->
    <bookstore>
      <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
          <name>Plato</name>
        </author>
        <price>9.99</price>
      </book>
    </bookstore>