How to: Use System.xml to merge data in both XML documents in Visual C # .NET

xiaoxiao2021-03-06  62

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.

Gambardella, Matthew

XML Developer's Guide </ Title></p> <p><genre> Computer </ genre></p> <p><Price> 44.95 </ price></p> <p></ book></p> <p><book id = "bk102"></p> <p><author> jeanette, dasha </ author></p> <p><title> QUACK The Duck </ Title></p> <p><genre> Fantasy </ GENRE></p> <p><price> 5.95 </ price></p> <p></ book></p> <p></ 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.</p> <p>Create Books2.xml files</p> <p>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'?></p> <p><catalog></p> <p><book id = "bk106"></p> <p><Author> Randall, Cynthia </ author></p> <p><title> Lover Birds </ Title></p> <p><genre> Romance </ GENRE></p> <p><price> 4.95 </ price></p> <p></ book></p> <p><book id = "bk107"></p> <p><Author> VinZovskaia, Irina </ author> <title> Piano Fort a </ Title></p> <p><genre> Romance </ GENRE></p> <p><price> 4.95 </ price></p> <p></ book></p> <p></ 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.</p> <p>Steps to create a Visual C # .NET application</p> <p>Start Visual Studio .NET and create a Visual C # console application project. Add the following code to the "Code" window Top: use system;</p> <p>USING SYSTEM.XML;</p> <p>Using system.io;</p> <p>Using system.data; copy the following code and paste it into static void main (String [] ARGS: TRY</p> <p>{</p> <p>XMLTextReader XmlReader1 = New XMLTextReader ("C: //Books1.xml");</p> <p>XMLTextReader XmlReader2 = New XMLTextReader ("C: //Books2.xml");</p> <p>DataSet DS = New Dataset ();</p> <p>S.Readxml (XmlReader1);</p> <p>DataSet DS2 = New Dataset ();</p> <p>DS2.Readxml (XmlReader2);</p> <p>DS.MERGE (DS2);</p> <p>DS.WRITEXML ("C: //Books.xml");</p> <p>Console.writeline ("Completed Merging XML Documents);</p> <p>}</p> <p>Catch (System.exception EX)</p> <p>{</p> <p>Console.write (ex.Message);</p> <p>}</p> <p>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.</p> <p>Different XML document programs</p> <p>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.</p> <p>Combined XML document with different structures</p> <p>Open Books2.xml, then replace the XML as the following XML: <? XML Version = "1.0"?></p> <p><Sampledata></p> <p><Customer></p> <p><Customername> Bob Frapples </ Customername></p> <p><PricePerUnit> 5.95 </ priceperUnit></p> <p><Closingdate> 2001-12-16 </ closingdate></p> <p></ Customer></p> <p><Customer></p> <p><Customername> Bob Frapples </ Customername></p> <p><PricePerUnit> 5.95 </ priceperUnit> <closingdate> 2001-12-16 </ closingdate></p> <p></ Customer></p> <p></ 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).</p> <p>The XML document with similar structures (the second document contains other elements)</p> <p>Open books2.xml, replace the XML of the following XML: <? XML Version = '1.0' Encoding = 'UTF-16'?></p> <p><catalog></p> <p><book id = "bk106"></p> <p><Author> Randall, Cynthia </ author></p> <p><title> Lover Birds </ Title></p> <p><genre> Romance </ GENRE></p> <p><price> 4.95 </ price></p> <p><Publish_Date> 2000-09-02 </ public_date></p> <p><Description> When Carla Meets Paul At An Ornithology</p> <p>Conference, Tempers Fly as feathers get rufled. </ description></p> <p></ book></p> <p><book id = "bk107"></p> <p><author> VinZovskaia, Irina </ author></p> <p><title> Piano Fort a </ Title></p> <p><genre> Romance </ GENRE></p> <p><price> 4.95 </ price></p> <p><Publish_Date> 2000-11-02 </ public_date></p> <p><Description> Two Young Pianists Strike a Chord WHEN THEY FIGHT A</p> <p>Pitched Battle to Stop The Metro Gnomes at a key base. </ description></p> <p></ book></p> <p></ 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.</p> <p>The XML document with similar structures (second document contains attributes)</p> <p>Open books2.xml, replace the XML of the following XML: <? XML Version = '1.0' Encoding = 'UTF-16'?></p> <p><catalog></p> <p><book id = "bk106" genre = "romance"></p> <p><Author> Randall, Cynthia </ author></p> <p><title> Lover Birds </ Title></p> <p><price> 4.95 </ price></p> <p></ book></p> <p><book id = "bk107" genre = "romance"> <author> Vinzovskaia, Irina </ author></p> <p><title> Piano Fort a </ Title></p> <p><price> 4.95 </ price></p> <p></ book></p> <p><book id = "bk108" genre = "Horror"></p> <p><author> de dogg, jazz </ author></p> <p><title> Night of the Flea </ Title></p> <p><price> 4.95 </ price></p> <p></ book></p> <p></ 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.</p> <p>Combined XML document with similar structure (the first document contains attributes)</p> <p>Modify the Visual C # code in the following ways to attach Books1.xml to Books2.xml: TRY</p> <p>{</p> <p>XMLTextReader XmlReader1 = New XMLTextReader ("C: //Books2.xml");</p> <p>XMLTextReader XmlReader2 = New XMLTextReader ("C: //Books1.xml");</p> <p>DataSet DS = New Dataset ();</p> <p>S.Readxml (XmlReader1);</p> <p>DataSet DS2 = New Dataset ();</p> <p>DS2.Readxml (XmlReader2);</p> <p>DS.MERGE (DS2);</p> <p>DS.WRITEXML ("C: //Books.xml");</p> <p>Console.writeline ("Completed Merging XML Documents);</p> <p>}</p> <p>Catch (System.exception EX)</p> <p>{</p> <p>Console.write (ex.Message);</p> <p>}</p> <p>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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-112148.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="112148" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'QwpJ1Sevw6_2FIPHNQk0AhQJKBfEWFumV93aMuRuOqJGv60qggA8zlFEPzhbYnNJk39R3van5zhjCNyUBSGV61Kw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>