How to: Use System.xml to merge data in both XML documents in Visual C # .NET
Summary This step-by-step guidance article explains how to use
DataSet object combines two XML documents. When using ADO.NET to support disconnect, distributed data schemes,
DataSet object is the key.
The dataset is the representation of data in memory, which provides a consistent relational programming model without having to care about the location of the data source.
The data set represents a complete set of data, including related tables, constraints, and relationships between tables.
Require the following list lists the recommended hardware, software, network structure, and service pack required:
Microsoft Windows XP, Windows 2000 or Windows NT 4.0 Service Pack 6A (SP6A) Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:
Visual C # .NET Syntax Scalable Markup Language (XML)
Create Books1.xml files
From the Windows Start menu, click Run, type NotePad.exe, and then click OK. Highlight the following code, right-click the highlighted content, and then click Copy. In the Notepad, on the Edit menu, click Paste. Xml Version = '1.0' encoding = 'UTF-16'?>
book>
book>
Catalog> On the File menu, click Save. In Save As dialog box, in the Save Type drop-down list box, click All files. In the File Name text box, type Books1.xml, and then click Save.
Create Books2.xml files
In the Notepad, create a text file called Books2.xml. Copy the following XML and paste it into Books2.xml: XML Version = '1.0' encoding = 'UTF-16'?>
book>
book>
Catalog> On the File menu, click Save. In Save As dialog box, in the Save Type drop-down list box, click All files. In the File Name text box, type Books2.xml, then click Save.
Steps to create a Visual C # .NET application
Start Visual Studio .NET and create a Visual C # console application project. Add the following code to the "Code" window Top: use system;
USING SYSTEM.XML;
Using system.io;
Using system.data; copy the following code and paste it into static void main (String [] ARGS: TRY
{
XMLTextReader XmlReader1 = New XMLTextReader ("C: //Books1.xml");
XMLTextReader XmlReader2 = New XMLTextReader ("C: //Books2.xml");
DataSet DS = New Dataset ();
S.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 (); generates and runs the application. Note that the message will be displayed in the Console window: Completed Merging XML Documents (XML document merge is completed). Close the Console window. Note that the books.xml file is created in the path you specified. Open books.xml. Note that the data in Books2.xml has been attached to the end of Books1.xml.
Different XML document programs
The Visual C # .NET example in front of the XML document with the same structure demonstrates the output of an XML document with the same structure.
Combined XML document with different structures
Open Books2.xml, then replace the XML as the following XML: XML Version = "1.0"?>
Customer>
Customer>
Sampledata> Save Books2.xml. Run the Visual C # .NET project again. Note that the node of the second document (books2.xml) is attached to the first XML document (Books1.xml).
The XML document with similar structures (the second document contains other elements)
Open books2.xml, replace the XML of the following XML: XML Version = '1.0' Encoding = 'UTF-16'?>
Conference, Tempers Fly as feathers get rufled. description>
book>
Pitched Battle to Stop The Metro Gnomes at a key base. description>
book>
Catalog> Save Books2.xml. Run the Visual C # .NET project again. Note that the node of the second document is attached to the first XML document.
The XML document with similar structures (second document contains attributes)
Open books2.xml, replace the XML of the following XML: XML Version = '1.0' Encoding = 'UTF-16'?>
book>
book>
book>
Catalog> Save Books2.xml. Run the Visual C # .NET project again. Note that the node in the second document has been attached to the first XML document, the structure is the same as the first XML document.
Combined XML document with similar structure (the first document contains attributes)
Modify the Visual C # code in the following ways to attach Books1.xml to Books2.xml: TRY
{
XMLTextReader XmlReader1 = New XMLTextReader ("C: //Books2.xml");
XMLTextReader XmlReader2 = New XMLTextReader ("C: //Books1.xml");
DataSet DS = New Dataset ();
S.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 (); Run the Visual C # .NET project again. Please note that the generated XML document attached to Books1.xml in Books2.xml. You will notice that all Book nodes contain "genre" properties. Therefore, according to the structure of the first XML document, it is necessary to modify the second XML document to make the generated XML more meaningful.