Lm th? no ? ch?nh s?a v lu XML v?i l?p XmlDocument trong cc.NET Framework SDK

D?ch tiu ? D?ch tiu ?
ID c?a bi: 301233 - Xem s?n ph?m m bi ny p d?ng vo.
Bi vi?t ny ? c?p ?n d?i y.NET Framework l?p Khng gian tn th vi?n:
  • System.xml
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

M?u ny minh ho? lm th? no ? C?p Nh?t v lu XML v?i cc XmlDocument l?p.

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 b?n ghi d?ch v? c?n thi?t:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, ho?c my ch? Windows NT 4.0
  • Microsoft Visual Studio.NET
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i sau cc ch? ?:
  • Thu?t ng? XML
  • T?o v ?c m?t t?p tin XML
  • M h?nh ?i t?ng ti li?u (DOM)

Lm th? no ? s? d?ng XmlDocument l?p ? ti?t ki?m XML

  1. T?o m?i Visual Basic ho?c C# giao di?n i?u khi?n ?ng d?ng trong Visual Studio.NET.
  2. H?y ch?c ch?n r?ng d? n tham chi?u khng gian tn System.Xml .
  3. S? d?ng cc bo co hng nh?p kh?u vo khng gian tn Xml ? b?n khng ph?i v?t qua v?ng lo?i t? khai XmlTextReader sau trong m? c?a b?n. B?n ph?i s? d?ng cc bo co hng nh?p kh?u tr?c ? b?t k? t? khai khc.
    Visual Basic.NET Code
    Imports System.Xml
    C# Code
    using System.Xml;
    
  4. T?o m?t l?p XmlDocument m?i, v s? d?ng phng php t?i ? t?i n.

    Cc l?p h?c XmlDocument ?i di?n cho cc ti li?u XML v c m?t phng php t?i ? t?i cc ti li?u t? m?t t?p tin, d?ng, ho?c m?t XmlReader.

    Visual Basic.NET Code
    Dim myXmlDocument as XmlDocument = new XmlDocument()
    myXmlDocument.Load ("books.xml")
    
    C# Code
    XmlDocument myXmlDocument = new XmlDocument();
    myXmlDocument.Load ("books.xml");
    
    Lu ? r?ng, m?c d cc t?p tin Books.xml ?c s? d?ng ? y, b?n c th? t?o t?p Books.xml ring c?a b?n. M?t t?p tin Books.xml m?u c?ng ?c bao g?m v?i tr?c quan Ph?ng thu.NET v.NET Framework ph?n m?m b? pht tri?n (SDK).
  5. ?i t?ng XmlNode cung c?p phng php v cc thu?c tnh ? thao tc m?t nt ch?n m?t. S? d?ng cc ?i t?ng XmlNode ti s?n DocumentElement c?a XmlDocument tr? v? ? thao tc m?t nt ch?n m?t XML.
    Visual Basic.NET Code
    Dim node as XmlNode
    node = myXmlDocument.DocumentElement
    
    C# Code
    XmlNode node;
    node = myXmlDocument.DocumentElement;
    
  6. Iterate qua cc tr? em c?a cc ph?n t? ti li?u, v T?m t?t c? cc nt ch?n m?t "gi". S? d?ng cho m?i looping xy d?ng v?i ti s?n ChildNodes c?a cc ?i t?ng nt ch?n m?t ? t?m t?t c? cc nt ch?n m?t m c m?t ti s?n tn l tng ng v?i "gi". Tng g?p i gi c?a cc cu?n sch.
    Visual Basic.NET Code
    Dim node2 As XmlNode 'Used for internal loop.
    Dim nodePriceText As XmlNode
    For Each node In node.ChildNodes
       'Find the price child node.
       For Each node2 In node.ChildNodes
          If node2.Name = "price" Then
             '                    nodePriceText = node2.InnerText
             Dim price As Decimal
             price = System.Decimal.Parse(node2.InnerText)
    
             ' Double the price.
             Dim newprice As String
             newprice = CType(price * 2, Decimal).ToString("#.00")
             Console.WriteLine("Old Price = " & node2.InnerText & Strings.Chr(9) & "New price = " & newprice)
             node2.InnerText = newprice
          End If
       Next
    Next
    
    C# Code
    foreach(XmlNode node1 in node.ChildNodes)
       foreach (XmlNode node2 in node1.ChildNodes)
          if (node2.Name == "price")
             {
                Decimal price = Decimal.Parse(node2.InnerText);
                // Increase all the book prices by 20%
                String newprice = ((Decimal)price*(new Decimal(1.20))).ToString("#.00");
                Console.WriteLine("Old Price = " + node2.InnerText + "\tNew price = " + newprice);
                node2.InnerText = newprice;
              }
    
  7. S? d?ng phng php luXmlDocument l?p ? lu cc thay ?i XML vo m?t t?p tin m?i ?c ?t tn InflatedPriceBooks.xml.

    B?n c th? s? d?ng phng php lu ? lu cc d? li?u XML ? t?p tin, su?i, v XmlWriters.
    Visual Basic.NET Code
    myXmlDocument.Save("InflatedPriceBooks.xml")
    
    C# Code
    myXmlDocument.Save("InflatedPriceBooks.xml");
    
  8. Xy d?ng b?t ln v ch?y d? n c?a b?n.

THAM KH?O

? bi?t thm chi ti?t, h?y truy c?p cc web site sau y:
XML trong.NET:.NET Framework XML l?p v C# cung c?p n gi?n, kh? nng m? r?ng d? li?u Manipulation

Document Object Model (Core) c?p 1

Document Object Model Core

XmlDocument l?p

XmlNode l?p

XML Document Object Model (DOM)

Thu?c tnh

ID c?a bi: 301233 - L?n xem xt sau cng: 12 Thang Sau 2012 - Xem xt l?i: 3.0
p d?ng
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
T? kha:
kbhowtomaster kbmt KB301233 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: 301233

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