Lm th? no ?: Xc ?nh v? tr v thay th? cc k? t? ?c bi?t trong m?t t?p XML Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 308060 - 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 m t? cch thay th? cc k? t? ?c bi?t trong m?t t?p tin Extensible Markup Language (XML) b?ng cch s? d?ng Visual Basic .NET.

M t? k? thu?t

XML predefines tham chi?u th?c th? nm sau cho ?c bi?t nhn v?t m n?u khng s? ?c hi?u l m?t ph?n c?a ngn ng? nh d?u:

Character Name Entity Reference Character Reference Numeric Reference
Ampersand & & &
Left angle bracket &lt; < &#38;#60;
Right angle bracket &gt; > &#62;
Straight quotation mark &quot; " &#39;
Apostrophe &apos; ' &#34;


B?n c th? s? d?ng ti li?u tham kh?o th?c th? v k? t? ? thot kh?i khung gc tri, d?u "v", v delimiters khc. B?n c?ng c th? dng s? k? t? ti li?u tham kh?o; h? ang m? r?ng ngay l?p t?c khi h? ?c cng nh?n v h? l ?c coi l d? li?u k? t?, v? v?y b?n c th? dng k? t? s? ti li?u tham kh?o

N?u b?n ang tuyn b? ho?c l sau hai th?c th?
  • LT
  • amp
b?n c th? tuyn b? h? thnh th?c th? n?i b? m thay th? vn b?n l m?t nhn v?t nhn v?t tng ?ng (tri gc ch? khung ho?c d?u "v") m ang ?c ch?y thot; thot kh?i i l yu c?u cho cc t? ch?c ? cc ti li?u tham kh?o cho h? s?n xu?t m?t t?t ?c h?nh thnh k?t qu?.

N?u b?n ang tuyn b? b?t c? sau ba th?c th?
  • gt
  • apos
  • quot
b?n c th? tuyn b? h? thnh th?c th? n?i b? m thay th? vn b?n l nhn v?t duy nh?t ang ?c thot.

Xc ?nh xem m?t s? thay th? nhn v?t ?c bi?t l b?t bu?c

Khng b?t bu?c: Trong d? li?u ?c l?y t? c s? d? li?u file XML

Khi b?n ang s? d?ng Microsoft.NET Framework, d? li?u L?y v lu tr? trong m?t S? li?u. Khi b?n ang vi?t d? li?u t? m?t S? li?u vo m?t t?p tin XML b?ng cch s? d?ng cc WriteXml phng php, cc k? t? ?c bi?t ny ?c nh?c ?n trong cc "Tm l?c" ph?n ?c thay th? b?ng cc ti li?u tham kh?o nhn v?t tng ?ng; V? v?y, khi b?n ang vi?t file XML v b?n ang s? d?ng m?t S? li?u, khng c qu tr?nh thay th? ?c bi?t l b?t bu?c.

B?t bu?c: M?t t?p XML v?i k? t? ?c bi?t

i khi cc t?p tin XML ho?c d? li?u XML l ?n t? m?t bn th? ba c th? s? d?ng cc k? t? ?c bi?t; trong tr?ng h?p ny, d? li?u t?o ra l?i khi b?n n?p n vo m?t XmlDocument ?i t?ng ho?c m?t XmlReader ?i t?ng.

L?i sau y ?c t?o ra khi cc d?u "v" nhn v?t l g?p ph?i:
L?i ? x?y ra trong khi phn tch c php entity_name, ?ng dy #, v? tr #.
ni d?ng # v v? tr # ?i di?n cho cc v? tr chnh xc c?a cc ?c bi?t k? t?.

L?i sau x?y ra khi m?t khung gc tri l g?p ph?i:
Cc '<' character,="" hexadecimal="" value="" 0x3c,cannot="" be="" included="" in="" a="" name.=""></'> #, v? tr #.
Trong thng bo l?i ny, d?ng # v v? tr # Khng ch? ra v? tr m t?n t?i gc khung bn tri, nhng trong l?n th? hai tri angle bracket l g?p ph?i.

N?u cc t?p tin XML ch?a m?t quy?n Angle bracket (&gt;), m?t bo gi nh d?u th?ng (") ho?c m?t apostrophe ('), nh?ng ?c x? l? b?i cc XmlReader v cc XmlDocument cc ?i t?ng v? ch? thay th? nhn v?t duy nh?t l c?n thi?t cho cc k? t? ny.

Thay th? cc k? t? ?c bi?t

? thay th? d?u "v" v cc k? t? bn tri gc ? khung:
  1. T?o t?p tin XML.
  2. T?o Visual Basic.NET ?ng d?ng, v sau chn M?.

T?o cc t?p tin XML

Sao chp v dn o?n m? sau vo Notepad, v sau lu cc t?p tin nh customers.xml:
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<CustomerID>BLAUS</CustomerID>
<CompanyName>Blauer See Delikatessen</CompanyName>
<ContactName>Hanna Moos</ContactName>
<Region>test<ing</Region>
</Customer>
<Customer>
<CustomerID>SPLIR</CustomerID>
<CompanyName>Split Rail Beer & Ale</CompanyName>
<ContactName>Art raunschweiger</ContactName>
<Region>WY</Region>
</Customer>
</Customers>
				

T?o Visual Basic.D? n NET

  1. T?o ra m?t m?i Visual Basic.NET Windows ?ng d?ng.
  2. S? d?ng m?t ho?t ?ng ko v th? ? di chuy?n m?t TextBoxhai Nt i?u khi?n, v m?t DataGrid.
  3. ?t cc Multiline ti s?n c?a cc TextBox ? S? th?t.
  4. Chuy?n nh?p khng gian tn sau y:
    Imports System.Xml
    Imports System.IO
    Imports System.Data.SqlClient
    					
  5. Sau khi cc ph?n sau y
    Inherits System.Windows.Forms.Form
    sao chp v dn m?u m? sau y:
     Dim filepath As String = "C:\customers.xml"
    Private Sub ReplaceSpecialChars(ByVal linenumber As Long)
            Dim strm As StreamReader
            Dim strline As String
            Dim strreplace As String
    
            Dim tempfile As String = "C:\temp.xml"
            Try
                FileCopy(filepath, tempfile)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
            Dim strmwriter As New StreamWriter(filepath)
            strmwriter.AutoFlush = True
    
            strm = New StreamReader(tempfile)
    
    
            Dim i As Long = 0
            While i < linenumber - 1
                strline = strm.ReadLine
                strmwriter.WriteLine(strline)
                i = i + 1
            End While
    
            strline = strm.ReadLine
            Dim lineposition As Int32
            lineposition = InStr(strline, "&")
            If lineposition > 0 Then
                strreplace = "&amp;"
            Else
                lineposition = InStr(2, strline, "<")
                If lineposition > 0 Then
                    strreplace = "<"
                End If
            End If
            strline = Mid(strline, 1, lineposition - 1) + strreplace + Mid(strline, lineposition + 1)
            strmwriter.WriteLine(strline)
    
            strline = strm.ReadToEnd
            strmwriter.WriteLine(strline)
    
            strm.Close()
            strm = Nothing
    
            strmwriter.Flush()
            strmwriter.Close()
            strmwriter = Nothing
    
        End Sub
    
        Public Function LoadXMLDoc() As XmlDocument
            Dim xdoc As XmlDocument
            Dim lnum As Long
            Dim pos As Long
            Dim Newxml As String
            Try
                xdoc = New XmlDocument()
                xdoc.Load(filepath)
            Catch ex As XmlException
                MessageBox.Show(ex.Message)
                lnum = ex.LineNumber
                ReplaceSpecialChars(lnum)
    
                xdoc = LoadXMLDoc()
    
            End Try
            Return (xdoc)
        End Function
    					
  6. Sao chp v dn o?n m? sau vo cc Nh?p vo s? ki?n c?a Button1:
    Dim xmldoc As New XmlDocument()
            xmldoc = LoadXMLDoc()
            Dim nextnode As XmlNode
            nextnode = xmldoc.FirstChild.NextSibling
            TextBox1.Text = nextnode.OuterXml
    					
  7. Sao chp v dn o?n m? sau vo cc Nh?p vo s? ki?n c?a Button2:
    Dim ds As New DataSet()
            Dim xdoc As New XmlDocument()
            Dim cnNwind As New SqlConnection("Data source=myservername;user id=myuser;Password=mypassword;Initial catalog=Northwind;")
            Dim daCustomers As New SqlDataAdapter("Select customerid,companyname,contactname, region from customers where region='WY'", cnNwind)
            Dim filepath As String
            Try
                daCustomers.Fill(ds, "Customers")
                DataGrid1.DataSource = ds.Tables(0)
                ds.WriteXml("C:\Dataset.xml")
                xdoc.Load("C:\Dataset.xml")
                Dim nextnode As XmlNode
                nextnode = xdoc.FirstChild.NextSibling
                TextBox1.Text = nextnode.OuterXml.ToString
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    					
  8. Thay ?i tn my ch?, tn ng?i dng v m?t kh?u ? k?t n?i t?i my ch? c?a b?n ang ch?y Microsoft SQL Server.
  9. Xy d?ng d? n, v sau ch?y n.
  10. Nh?p vo Button1.

    Cc l?i m b?n nh?n ?c l ph h?p v?i cc m t? v? cc l?i ?c gi?i thch trong cc "yu c?u: m?t t?p XML v?i K? t? ?c bi?t"ph?n. Cc d? li?u XML sau ?c hi?n th? trong cc TextBox; d?u "v" ?c thay th? b?ng
  11. B?m vo Button2.

    Trong cc DataGrid, companyname c m?t d?u "v" v cc TextBox hi?n th? d? li?u XML v?i

THAM KH?O

? bi?t thm chi ti?t, nh?p vo s? bi vi?t d?i y ? Xem bi vi?t trong c s? ki?n th?c Microsoft:
251354 Lm th? no ?: Xc ?nh v? tr v thay th? cc k? t? ?c bi?t trong m?t ti li?u XML v?i Visual Basic

Thu?c tnh

ID c?a bi: 308060 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
T? kha:
kbgrpdsmsxml kbhowtomaster kbmsxml kbmt KB308060 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:308060

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