Lm th? no ? xc ?nh v? tr v thay th? cc k? t? ?c bi?t trong m?t t?p XML v?i Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 316063 - 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 C#.NET.

M t? k? thu?t

XML predefines tham chi?u th?c th? nm sau cho k? t? ?c bi?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? s? d?ng k? t? s? ti li?u tham kh?o. S? k? t? ti li?u tham kh?o ?c m? r?ng ngay l?p t?c khi h? ?c cng nh?n. Ngoi ra, v? nhn v?t s? ti li?u tham kh?o ?c ?i x? nh d? li?u k? t?, b?n c th? s? d?ng ti li?u tham kh?o nhn v?t so

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

N?u b?n tuyn b? b?t k? c?a ba th?c th? sau y:
  • >
  • '
  • "
b?n ph?i tuyn b? h? thnh n?i b? th?c th? c vn b?n thay th? l nhn v?t duy nh?t m ang ?c thot.

Xc ?nh cho d b?n ph?i thay th? m?t k? t? ?c bi?t

Khng b?t bu?c: t?p tin XML trong d? li?u ?c l?y t? c s? d? li?u

Khi b?n ang s? d?ng Microsoft.NET Framework, d? li?u ?c l?y v ?c lu tr? trong m?t S? li?u ?i t?ng. Khi b?n 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 ?c nh?c ?n trong ph?n "Tm l?c" ?c thay th? b?ng ti li?u tham kh?o nhn v?t tng ?ng. V? v?y, khi b?n vi?t file XML, v n?u b?n 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: XML t?p tin c ch?a d? li?u XML bn th? ba v?i k? t? ?c bi?t

i khi cc t?p tin XML ho?c d? li?u XML, m xu?t pht 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.

B?n nh?n ?c thng bo l?i sau khi k? t? d?u "v" l g?p ph?i:
L?i x?y ra 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 k? t? ?c bi?t.

B?n nh?n ?c thng bo l?i sau 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 # ch? ra v? tr ni tri angle bracket t?n t?i, nhng ni th? hai bn tri angle bracket l g?p ph?i.

N?u cc t?p tin XML ch?a m?t gc bn ph?i khung (&gt;), m?t bo gi nh d?u th?ng (") ho?c m?t apostrophe ('), cc XmlReader v cc XmlDocument cc ?i t?ng x? l? cc ?i t?ng ny v? cc k? t? ?i h?i ph?i ch? duy nh?t k? t? thay th?.

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 C#.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 t?p tin l 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 C#.L?I d? n

  1. T?o ra m?t m?i Visual C#.NET ?ng d?ng Windows nh sau:
    1. B?t ?u Microsoft Visual Studio.NET.
    2. Trn cc T?p tin tr?nh n, i?m ?n M?i, sau b?m D? n.
    3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual C# cc d? n d?i Cc lo?i d? n, sau b?m ?ng d?ng Windows d?i M?u.
  2. Ko m?t TextBox ki?m sot, hai Nt i?u khi?n, v m?t DataGrid i?u khi?n t? h?p cng c? ? h?nh th?c m?c ?nh c?a b?n, Form1.cs.
  3. ?t cc Multiline ti s?n c?a cc TextBox ? S? th?t.
  4. Chuy?n nh?p khng gian tn sau y:
    using System.Xml;
    using System.IO;
    using System.Data.SqlClient;
    					
  5. Thm m? sau y sau khi cc Chnh ch?c nng:
    string filepath = "C:\\Customers.xml";
    private void ReplaceSpecialChars(long linenumber)
            {
                System.IO.StreamReader strm;
                string strline;
                string strreplace = " ";
                string tempfile = "C:\\Temp.xml";
                try
                {
                    System.IO.File.Copy(filepath,tempfile,true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
                StreamWriter strmwriter = new StreamWriter(filepath);
                strmwriter.AutoFlush = true;
                strm = new StreamReader(tempfile);
                long i  = 0;
                while (i < linenumber - 1)
                {
                    strline = strm.ReadLine();
                    strmwriter.WriteLine(strline);
                    i = i + 1;
                }
    
                strline = strm.ReadLine();
                Int32 lineposition;
    		
                lineposition = strline.IndexOf("&");
                if (lineposition > 0) 
                {
                    strreplace = "&amp;";
                }
                else
                {
                    lineposition = strline.IndexOf("<",1);
                    if (lineposition > 0 )
                    {
                        strreplace = "<";
                    }
    
                }
                strline = strline.Substring(0, lineposition - 1) + strreplace + strline.Substring(lineposition + 1);
                strmwriter.WriteLine(strline);
    
                strline = strm.ReadToEnd();
                strmwriter.WriteLine(strline);
    
                strm.Close();
                strm = null;
    
                strmwriter.Flush();
                strmwriter.Close();
                strmwriter = null;
    
            }
    		
            public XmlDocument LoadXMLDoc() 
            {
            XmlDocument xdoc;
            long lnum;
    		
            try
            {
                xdoc = new XmlDocument();
                xdoc.Load(filepath);
            }
            catch (XmlException ex)
            {
                MessageBox.Show(ex.Message);
                lnum = ex.LineNumber;
                ReplaceSpecialChars(lnum);
    
                xdoc = LoadXMLDoc();
            }
        return (xdoc);
        }
    					
  6. Thm m? sau y ? cc Button1_Click s? ki?n:
    XmlDocument xmldoc = new XmlDocument();
            xmldoc = LoadXMLDoc();
            XmlNode nextnode; 
            nextnode = xmldoc.FirstChild.NextSibling;
            this.textBox1.Text = nextnode.OuterXml.ToString();
    					
  7. Thm m? sau y ? cc Button2_Click s? ki?n:
    DataSet ds = new DataSet();
                XmlDocument xdoc = new XmlDocument();
                SqlConnection cnNwind = new SqlConnection("Data source=myServerName;user id=myUser;Password=myPassword;Initial catalog=Northwind;");
                SqlDataAdapter daCustomers = new SqlDataAdapter("Select customerid,companyname,contactname, region from customers where region='WY'", cnNwind);
                string filepath = "C:\\Customers.xml";
                try
                {
                    daCustomers.Fill(ds, "Customers");
                    this.dataGrid1.DataSource = ds.Tables["Customers"];
                    ds.WriteXml(filepath);
                    xdoc.Load(filepath);
                    XmlNode nextnode; 
                    nextnode = xdoc.FirstChild.NextSibling;
                    textBox1.Text = nextnode.OuterXml.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    					
  8. Thay ?i thu?c tnh trong cc SqlConnection k?t n?i chu?i nh l c?n thi?t cho mi tr?ng c?a b?n.
  9. Xy d?ng v ch?y cc d? n.
  10. Nh?p vo Button1.

    Cc l?i m b?n nh?n ?c l ph h?p v?i m t? l?i ?c gi?i thch trong cc B?t bu?c: M?t t?p XML v?i k? t? ?c bi?t ke tiep. Cc d? li?u XML s? xu?t hi?n trong cc TextBox; d?u "v" ?c thay th? b?ng
  11. Nh?p vo Button2.

    Trong cc DataGrid, nh?n th?y r?ng companyname c m?t d?u "v" v r?ng 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 sau y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
251354Lm 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: 316063 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
T? kha:
kbgrpdsmsxml kbhowtomaster kbmsxml kbmt KB316063 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:316063

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