Lm th? no ?: BETA: nh?p d? li?u t? hai XML ti li?u b?ng cch s? d?ng System.Xml v?i Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 308066 - 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 ny ch?ng t? lm th? no ? s? d?ng m?t S? li?u ?i t?ng ? h?p nh?t hai ti li?u XML. Cc S? li?u ?i t?ng l trung tm h? tr? k?ch b?n b? ng?t k?t n?i, phn ph?i d? li?u v?i ADO.NET. Cc S? li?u l m?t ?i di?n b? nh? c tr d? li?u cung c?p m?t m h?nh l?p tr?nh ph h?p, quan h? khng phn bi?t c?a ngu?n d? li?u. Cc S? li?u ?i di?n cho m?t b? hon ch?nh cc d? li?u, trong c lin quan ?n b?ng, rng bu?c v m?i quan h? gi?a cc b?ng.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi 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 Basic.NET c php
  • Extensible Markup Language (XML)

T?o t?p tin Books1.xml

  1. T? cc c?a s? B?t ?u tr?nh n, nh?p vo Ch?y, lo?i Notepad.exe, sau b?m Ok.
  2. Lm n?i b?t m? sau y, b?m chu?t ph?i vo o?n code v sau nh?p vo Sao. Trong Notepad, trn cc Ch?nh s?a tr?nh n, nh?p vo 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>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
       </book>
    </catalog>
    					
  3. Trn cc Tp tr?nh n, nh?p vo Lu.
  4. Trong cc Lu lam h?p tho?i h?p, trong cc Lu nh ki?u h?p danh sch th?-xu?ng, b?m vo T?t c? cc t?p. Trong cc Tn tp h?p vn b?n, lo?i Books1.xml, sau b?m Lu.

T?o t?p tin Books2.xml

  1. Trong Notepad, t?o m?t t?p tin vn b?n m?i tn l 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>Thurman, Paula</author>
          <title>Splish Splash</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
    </catalog>
    					
  3. Trn cc Tp tr?nh n, nh?p vo Lu.
  4. Trong cc Lu lam h?p tho?i h?p, trong cc Lu nh ki?u h?p danh sch th?-xu?ng, b?m vo T?t c? cc t?p. Trong cc Tn tp h?p vn b?n, lo?i Books2.xml, sau b?m Lu.

Cc b?c ? t?o Visual Basic.NET Application

  1. B?t ?u Visual Studio.NET, v t?o ra m?t d? n m?i c?a ?ng d?ng giao di?n i?u khi?n Visual Basic.
  2. Thm m? sau vo pha trn c?a c?a s? m?:
    Imports System.Xml
    					
  3. Sao chp v dn o?n m? sau trong cc Ph? chnh th? t?c:
            Dim xmlreader1 As New XmlTextReader("C:\Books1.xml")
    
            Dim xmlreader2 As New XmlTextReader("C:\Books2.xml")
    
            Dim ds1 As New DataSet()
            Try
                ds1.ReadXml(xmlreader1)
    
                Dim ds2 As New DataSet()
                ds2.ReadXml(xmlreader2)
    
                ds1.Merge(ds2)
                ds1.WriteXml("C:\Books.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()
    					
  4. Xy d?ng v ch?y cc ?ng d?ng. Thng bo r?ng "? hon thnh vi?c sp nh?p ti li?u XML" tin nh?n 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 ?ng d?n ? ch? ?nh.
  6. M? Books.xml. Ch ? 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 Basic tr?c.NET m?u ch?ng t? ra c?a ti li?u XML v?i cng m?t c?u trc.

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>Bill Gates</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    <Customer>
              <CustomerName>Bill Gates</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    </SampleData>
    					
  2. Lu Books2.xml.
  3. Ch?y Visual Basic.NET d? n m?t l?n n?a. Ch ? r?ng cc nt 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>Thurman, Paula</author>
          <title>Splish Splash</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-11-02</publish_date>
          <description>A deep-sea diver finds true love twenty 
          thousand leagues beneath the sea.</description>
       </book>
    </catalog>
    					
  2. Lu Books2.xml.
  3. Ch?y Visual Basic.NET d? n m?t l?n n?a. Ch ? r?ng cc nt 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>Thurman, Paula</author>
          <title>Splish Splash</title>
          <price>4.95</price>
       </book>
       <book id="bk108" genre="Horror">
          <author>Knorr, Stefan</author>
          <title>Creepy Crawlies</title>
          <price>4.95</price>
       </book>
       </catalog>
    					
  2. Lu Books2.xml.
  3. Ch?y Visual Basic.NET d? n m?t l?n n?a. Ch ? r?ng cc nt 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 Basic m? nh sau ? cho Books1.xml ?c g?n vo ? Books2.xml:
            Dim xmlreader1 As New XmlTextReader("C:\Books2.xml")
    
            Dim xmlreader2 As New XmlTextReader("C:\Books1.xml")
    
            Dim ds1 As New DataSet()
            Try
                ds1.ReadXml(xmlreader1)
    
                Dim ds2 As New DataSet()
                ds2.ReadXml(xmlreader2)
    
                ds1.Merge(ds2)
                ds1.WriteXml("C:\Books.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()
    					
  2. Ch?y Visual Basic.NET d? n m?t l?n n?a. Ch ? r?ng k?t qu? ti li?u XML n?i cc nt t? Books1.xml ? Books2.xml. Ngoi ra, thng bo r?ng t?t c? cc nt 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, th? hai ti li?u XML l?n ? cho 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 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: 308066 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
T? kha:
kbhowtomaster kbmt KB308066 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:308066

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