Lm th? no ? nh?p d? li?u t? hai ti li?u XML b?ng cch s? d?ng System.Xml b?ng cch s? d?ng Visual C# .net

D?ch tiu ? D?ch tiu ?
ID c?a bi: 311530 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

Tm t?t

Bi vi?t b?c ny cho b?n th?y lm th? no ? s? d?ng m?t ?i t?ng s? li?u ? h?p nh?t hai ti li?u XML. S? li?u ?i t?ng l trung tm ? h? tr? cc ng?t k?t n?i, phn ph?i d? li?u k?ch b?n v?i ADO.NET. S? li?u l m?t ?i di?n b? nh? c tr c?a d? li?u m cung c?p m?t m h?nh l?p tr?nh ph h?p, quan h? b?t k? cc ngu?n d? li?u. S? li?u ?i di?n cho m?t b? hon ch?nh cc d? li?u, trong c lin quan ?n b?ng, gi?i h?n trn, v m?i quan h? gi?a cc b?ng.

Yu c?u

Danh sch sau phc th?o ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi b?n ghi d?ch v? m b?n c?n:
  • Microsoft Windows XP, Windows 2000 ho?c Windows NT 4.0 Service Pack 6a (SP6a)
  • Microsoft Visual Studio .net
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • Visual C# .net c php
  • Extensible Markup Language (XML)

T?o ra cc t?p tin Books1.xml

  1. T? menu Windows B?t ?u , b?m ch?y, lo?i Notepad.exe, v sau b?m OK.
  2. Lm n?i b?t m? sau y, b?m chu?t ph?i vo o?n m?, v sau b?m sao. Trong Notepad, trn Edit menu, b?m dn.
    <?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>
    					
  3. Trn menu t?p , b?m lu.
  4. Trong Ti?t ki?m nh l hp thoai, trong h?p danh sch th? xu?ng Lu nh ki?u , nh?p vo T?t c? cc t?p tin. Trong h?p vn b?n File Name , g? Books1.xml, v sau nh?p vo lu.

T?o ra cc t?p tin Books2.xml

  1. Trong Notepad, t?o m?t t?p tin vn b?n m?i c tn Books2.xml.
  2. Sao chp v dn XML sau vo 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>
    					
  3. Trn menu t?p , b?m lu.
  4. Trong Ti?t ki?m nh l hp thoai, trong h?p danh sch th? xu?ng Lu nh ki?u , nh?p vo T?t c? cc t?p tin. Trong h?p vn b?n File Name , g? Books2.xml, v sau nh?p vo lu.

Cc b?c ? t?o Visual C# net Application

  1. B?t ?u Visual Studio net, v sau t?o ra m?t d? n m?i Visual C# giao di?n i?u khi?n ?ng d?ng.
  2. Thm m? sau vo pha trn c?a c?a s? m?:
    using System;
    using System.Xml;
    using System.IO;
    using System.Data ;
    					
  3. Sao chp v dn o?n m? sau trong static void Main (string [] args):
    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();	
    					
  4. Xy d?ng b?t ln v ch?y cc ?ng d?ng. Thng bo r?ng "? hon thnh vi?c sp nh?p cc ti li?u XML" tin th thoai s? xu?t hi?n trong c?a s? giao di?n i?u khi?n.
  5. ng c?a s? giao di?n i?u khi?n. Ch ? r?ng cc t?p tin Books.xml ?c t?o ra trong con ?ng m b?n ch? ?nh.
  6. M? Books.xml. Thng bo r?ng cc d? li?u t? Books2.xml ?c g?n vo cu?i cng ? Books1.xml.

K?ch b?n ti li?u XML khc nhau

K?t h?p ti li?u XML c cng c?u trc

Visual C# .net tr?c m?u ch?ng t? ra c?a ti li?u XML v?i c?u trc tng t?.

K?t h?p ti li?u XML c c?u trc khc nhau

  1. M? Books2.xml v thay th? cc XML v?i XML sau y:
    <?xml version="1.0" ?>
    <SampleData>
    <Customer>
              <CustomerName>Bob Frapples</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    <Customer>
              <CustomerName>Bob Frapples</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    </SampleData>
    					
  2. Lu Books2.xml.
  3. Ch?y cc d? n Visual C# .net m?t l?n n?a. Thng bo r?ng cc nt ch?n m?t t? ti li?u th? hai (Books2.xml) ?c n?i thm vo ti li?u XML ?u tin (Books1.xml).

K?t h?p ti li?u XML v?i c?u trc tng t? nh ni ti li?u th? hai ch?a cc y?u t? b? sung

  1. M? Books2.xml v thay th? cc XML v?i XML sau y:
    <?xml version="1.0"?>
    <catalog>
    <book id="bk106">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-09-02</publish_date>
          <description>When Carla meets Paul at an ornithology 
          conference, tempers fly as feathers get ruffled.</description>
       </book>
       <book id="bk107">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-11-02</publish_date>
          <description>Two young pianists strike a chord when they fight a 
          pitched battle to stop the gnomes at a key base.</description>
       </book>
    </catalog>
    					
  2. Lu Books2.xml.
  3. Ch?y cc d? n Visual C# .net m?t l?n n?a. Thng bo r?ng cc nt ch?n m?t t? ti li?u th? hai ?c n?i thm vo ti li?u XML ?u tin.

K?t h?p ti li?u XML v?i c?u trc tng t? nh ni ti li?u th? hai ch?a cc thu?c tnh

  1. M? Books2.xml v thay th? cc XML v?i XML sau y:
    <?xml version="1.0"?>
    <catalog>
    <book id="bk106" genre="Romance">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <price>4.95</price>
       </book>
       <book id="bk107" genre="Romance">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <price>4.95</price>
       </book>
       <book id="bk108" genre="Horror">
          <author>de Dogg, Jazz</author>
          <title>Night of the Flea</title>
          <price>4.95</price>
       </book>
       </catalog>
    					
  2. Lu Books2.xml.
  3. Ch?y cc d? n Visual C# .net m?t l?n n?a. Ch ? r?ng cc nt ch?n m?t t? ti li?u th? hai ?c n?i thm vo ti li?u XML ?u tin, v c?u trc l tng t? nh ti li?u XML ?u tin.

K?t h?p ti li?u XML v?i c?u trc tng t? nh ni ?u tin ti li?u ch?a cc thu?c tnh

  1. Thay ?i Visual C# M? nh sau ? cho Books1.xml ?c g?n vo ? Books2.xml:
    try
    	{
    		XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books2.xml");
    		XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books1.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();	
    					
  2. Ch?y cc d? n Visual C# .net m?t l?n n?a. Thng bo r?ng cc ti li?u XML k?t qu? p cc nt ch?n m?t t? Books1.xml ? Books2.xml. Ngoi ra, thng bo r?ng t?t c? cc nt ch?n m?t cu?n sch ch?a cc thu?c tnh "th? lo?i".

    V? v?y, ty thu?c vo c?u trc c?a ti li?u XML ?u tin, cc ti li?u XML th? hai l?n ? cho cc k?t qu? XML l c ? ngh?a hn.

Tham kh?o

? bi?t thm chi ti?t, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
286817 Lm th? no ? nh?p d? li?u trong ti li?u XML hai b?i l?p tr?nh m h?nh ?i t?ng ti li?u (DOM)

Thu?c tnh

ID c?a bi: 311530 - L?n xem xt sau cng: 30 Thang Tam 2012 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
T? kha:
kbhowtomaster kbio kbsystemdata kbmt KB311530 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 311530

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com