0120如何合併兩個使用 System.Xml 使用 Visual C#.NET 的 XML 文檔中的數據

http://support.microsoft.com/kb/311530/zh-cnide

Books1.xml

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <author>Jeanette, Dasha</author>
      <title>Quack the Duck</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
</catalog>
        

Books2.xml

<?xml version="1.0"?>
<catalog>
<book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
   <book id="bk107">
      <author>Vinzovskaia, Irina</author>
      <title>Piano Fort A</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
</catalog>

C#.NETui

using System;
using System.Xml;
using System.IO;
using System.Data ;
            
try
    {
        XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
        XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");

        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader1);
        DataSet ds2 = new DataSet();
        ds2.ReadXml(xmlreader2);
        ds.Merge(ds2);
        ds.WriteXml("C:\\Books.xml");
        Console.WriteLine("Completed merging XML documents");
    }
    catch (System.Exception ex)
    {
        Console.Write(ex.Message);
    }
Console.Read();  
相關文章
相關標籤/搜索