Lm th? no ? c tr m?t TreeView control v?i d? li?u XML trong Visual C# 2005 ho?c trong Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 317597 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i m?t Microsoft Visual Basic.NET Phin b?n c?a bi vi?t ny, xem 308063.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


B?c theo b?c bi ny m t? lm th? no ? c m?t TreeView ki?m sot b?ng cch s? d?ng d? li?u Extensible Markup Language (XML) trong Microsoft Visual C# 2005 ho?c trong Microsoft Visual C#.NET. B?i v? c? hai XML v cc TreeView ki?m sot ?i di?n cho cc d? li?u trong m?t ?nh d?ng c?u trc, cc TreeView ki?m sot l m?t s? l?a ch?n t? nhin ? hi?n th? d? li?u XML.

Cc TreeView ki?m sot c m?t Cc nt b? su t?p v?i g?c TreeNode cc ?i t?ng. M?i TreeNode l?n l?t c ring c?a m?nh Cc nt b? su t?p ch?a nhi?u hn m?t ?a tr? TreeNode.

Chu y M?u ny s? d?ng Document Object Model (DOM) phn tch cc l?p h?c c?a.NET ? qu tr?nh XML.

? bi?t thm v? thi?t k? c?a XML trong.NET Framework, xem cc "Tham khao"ph?n.

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, Microsoft Windows 2000 ho?c Microsoft Windows NT 4.0 gi d?ch v? 6a
  • D? li?u Microsoft Access Components 2.6 (MDAC) ho?c sau ny
  • Microsoft Visual Studio 2005 ho?c Microsoft Visual Studio.NET
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • Visual C# 2005 c php ho?c Visual C#.NET c php
  • XML v cc tiu chu?n lin quan
  • Windows Forms

Cc b?c ? t?o v c TreeView Control v?i XML

  1. Dn sau y, XML m?u m? trong m?t t?p tin vn b?n m?i ?t tn l "Sample.xml". T?p tin ny l d? li?u XML m?u v d? ny:
    <?xml version="1.0"?>
             <parent>id="cousin sister"
                <child>id="second cousin"</child>
             <child>id="cousin brother"</child>
  2. T?o m?t ?ng d?ng Windows trn m?i trong Visual C# 2005 ho?c trong Visual C#.NET. Form1 s? ?c thm vo cc ?ng d?ng theo m?c ?nh.
  3. Ko m?i TreeView, Nt, Nh?n hi?u, v TextBox i?u khi?n vo Form1.
  4. Thm m?u m? sau vo cu?i cc b?ng cch s? d?ng ch? th? ph?n t?i Form1.cs:
    using System.Xml;
  5. Dn o?n m? m?u sau vo cc Form1_Load s? ki?n:
             // Initialize the controls and the form.
             label1.Text = "File Path";
             label1.SetBounds(8, 8, 50, 20);
             textBox1.Text = Application.StartupPath + "\\Sample.xml";
             textBox1.SetBounds(64, 8, 256, 20);
             button1.Text = "Populate the TreeView with XML";
             button1.SetBounds(8, 40, 200, 20);
             this.Text = "TreeView control from XML";
             this.Width = 336;
             this.Height = 368;
             treeView1.SetBounds(8, 72, 312, 264);
  6. Dn o?n m? sau vo cc Button1_Click s? ki?n:
                // SECTION 1. Create a DOM Document and load the XML data into it.
                XmlDocument dom = new XmlDocument();
                // SECTION 2. Initialize the TreeView control.
                treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
                TreeNode tNode = new TreeNode();
                tNode = treeView1.Nodes[0];
                // SECTION 3. Populate the TreeView with the DOM nodes.
                AddNode(dom.DocumentElement, tNode);
             catch(XmlException xmlEx)
             catch(Exception ex)
  7. Dn o?n m? m?u sau sau khi cc Button1_Click s? ki?n:
          private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
             XmlNode xNode;
             TreeNode tNode;
             XmlNodeList nodeList;
             int i;
             // Loop through the XML nodes until the leaf is reached.
             // Add the nodes to the TreeView during the looping process.
             if (inXmlNode.HasChildNodes)
                nodeList = inXmlNode.ChildNodes;
                for(i = 0; i<=nodeList.Count - 1; i++)
                   xNode = inXmlNode.ChildNodes[i];
                   inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                   tNode = inTreeNode.Nodes[i];
                   AddNode(xNode, tNode);
                // Here you need to pull the data from the XmlNode based on the
                // type of node, whether attribute values are required, and so forth.
                inTreeNode.Text = (inXmlNode.OuterXml).Trim();
  8. Nh?n F5 ? xy d?ng v ch?y cc ?ng d?ng. H?y ch?c ch?n r?ng ?ng d?n c?a t?p XML l chnh xc v sau b?m cc nt. Cc d? li?u XML s? xu?t hi?n trong cc TreeView ki?m sot.
Chu y Cc ngu?n ti nguyn c th? l m?t t?p tin, m?t URL ho?c m?t d?ng su?i XML. Xem cc"Tham khao"ph?n dnh cho thng tin v? vi?c s? d?ng cc XmlDocument l?p ? t?i d? li?u XML t? cc ngu?n l?c khc nhau.

Cc b?c ? c tr cc TreeView Control v?i d? li?u yu c?u

M?u m? tr?c b?n ? d? li?u cy XML tr?c ti?p vo cc TreeView v hi?n th? t?t c? cc d? li?u. Ngoi ra, b?n c th? thm thm thng tin ? hi?n th? ho?c b? qua khng mong mu?n d? li?u.

Trong nhi?u tr?ng h?p, b?n c th? mu?n hi?n th? ch? l m?t ph?n c?a d? li?u XML. Cc ph?n c?a cc d? li?u m b?n mu?n hi?n th? thng t? ?ng xy d?ng, k?t qu? c?a m?t Extensible Stylesheet ngn ng? (XSL) chuy?n ?i, ho?c k?t qu? c?a m?t truy v?n XPath. i?u ny ph?n m t? lm th? no ? xy d?ng m?t ti li?u XML m?i v?i ch? l cc nt yu c?u v sau thm ti li?u m?i ? cc TreeView ki?m sot.

V d?, l?y cc b?c sau ch? cc y?u t? c?a con c?a d? li?u XML originial b?ng cch s? d?ng truy v?n XPath, v sau thm danh sch ny l m?t nt m?i ? cc TreeView.
  1. Dn sau m? ch? tr?c khi cc TreeView1.ExpandAll dy trong m?u tr?c :
                // SECTION 4. Create a new TreeView Node with only the child nodes.
                XmlNodeList nodelist = dom.SelectNodes("//child");
                XmlDocument cDom = new XmlDocument();
                foreach(XmlNode node in nodelist)
                   XmlNode newElem = cDom.CreateNode(XmlNodeType.Element, node.Name, node.LocalName);
                   newElem.InnerText = node.InnerText;
                treeView1.Nodes.Add(new TreeNode(cDom.DocumentElement.Name));
                tNode = treeView1.Nodes[1];
                AddNode(cDom.DocumentElement, tNode);
  2. Xy d?ng v sau ch?y cc ?ng d?ng. ?ng d?ng ny nn hi?n th? m?t "tr? em" nt g?c m?i trong cc TreeView thm vo d? li?u g?c.


? bi?t thm thng tin, h?y b?m vao s? bi vi?t sau ? xem bi vi?t trong C s? Ki?n th?c Microsoft:
313651L? tr?nh cho XML trong cc.NET Framework
? bi?t thm chi ti?t, h?y truy c?p trang Web Microsoft Developer Network (MSDN) sau y:
http://msdn2.Microsoft.com/en-US/Library/System.xml (vs.71) .aspx


Thu?c tnh

ID c?a bi: 317597 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
T? kha:
kbbcl kbctrl kbhowtomaster kbwindowsforms kbmt KB317597 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:317597

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