How to populate a TreeView control with XML data in Visual C# 2005 or in Visual C# .NET

???? ?????? ???? ??????
???? ID: 317597 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? ???? ?? Microsoft Visual Basic .NET ??????? ?? ???, ?????308063.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

This step-by step article describes how to populate aTreeViewcontrol by using Extensible Markup Language (XML) data in Microsoft Visual C# 2005 or in Microsoft Visual C# .NET. Because both XML and theTreeViewcontrol represent the data in a hierarchical format, theTreeViewcontrol is a natural choice to display XML data.

TheTreeViewcontrol has a???????? ??????TreeNode???????? ??? ????????TreeNode???? ??? ?? ???????????? ?? ???? ?????? ???? ??????TreeNode.

???:?? ????? ???????? ???????? ???? (DOM) XML ????????? ???? ?? ??? .NET ?? ???? ?? ????? ???? ?? ????? ???? ???

???? ??????? ?? ??? ??????? ?? ???? ??? XML .NET Framework ??? ??, ????? "??????"????

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows XP, Microsoft Windows 2000 ?? Microsoft Windows NT 4.0 ?????? ??? 6a
  • Microsoft ???? ?????? ????? 2.6 (MDAC) ?? ??? ???
  • Microsoft Visual Studio 2005 ?? Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ????? C# 2005 ???????? ?? ????? C# .NET ????????
  • XML ?? ??????? ????
  • Windows ???????

????? ?? XML ?? ??? TreeView ???????? ????????? ???? ?? ??? ???

  1. ??? ??? ???? "Sample.xml" ?? ??? ??? ????? XML ????? ??? ?? ???????? ?? ????? ?? ?? ?????? ?? ??? ????? XML ???? ??:
    <?xml version="1.0"?>
    <family>
    <parent>id="grandfather"
        <parent>id="father"
             <parent>id="brother"
                <child>id="niece"
                </child>
             </parent>
             <parent>id="me"
                <child>id="son"</child>
                <child>id="daughter"</child>
             </parent>
             <child>id="sister"</child>
         </parent>
         <parent>id="uncle"
             <parent>id="cousin sister"
                <child>id="second cousin"</child>
             </parent>
             <child>id="cousin brother"</child>
         </parent>
    </parent>
    </family>
  2. ????? C# 2005 ?? Visual C# .NET ??? ?? ?? Windows-?????? ????????? ?????? Form1 ???????? ??? ?? ?? ????????? ?? ??? ????? ???
  3. ??? ??????TreeView,???,????, ????? ?????Form1 ?? ???????? ???
  4. ????? ????? ??? ?? ??? ??? ??????? ????? ????Form1.cs ??? directives ??????:
    using System.Xml;
    					
  5. ??? ????? ????? ??? ?? ???????Form1_Load?????:
          
             // 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. ??? ????? ??? ???????Button1_Click?????:
             try 
             {
                // SECTION 1. Create a DOM Document and load the XML data into it.
                XmlDocument dom = new XmlDocument();
                dom.Load(textBox1.Text);
    
                // SECTION 2. Initialize the TreeView control.
                treeView1.Nodes.Clear();
                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);
                treeView1.ExpandAll();
             }
             catch(XmlException xmlEx)
             {
                MessageBox.Show(xmlEx.Message);
             }
             catch(Exception ex)
             {
                MessageBox.Show(ex.Message);
             }
    					
  7. ???? ??? ????? ????? ??? ?? ???????Button1_Click?????:
          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);
                }
             }
             else
             {
                // 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. ????? ?? ????????? ?? ????? ?? ??? F5 ?????? ????????? ???? ?? XML ???? ?? ??? ??, ?? ???? ??? ????? ???????. XML ???? ??? ?????? ?????TreeView???????? ???
???:?????? ?? ??? ?????, ???? URL ?? ???? XML ??????? ???? ?? ???? ????? "??????"??? XmlDocument ???? ?? ????? ?? XML ???? ?? ????? ???????? ?? ??? ???? ?? ???? ??? ??????? ?? ????

?????? ???? ?? ??? TreeView ???????? ????????? ???? ?? ??? ???

????? ??? ????? ???? ?? XML ???? ???? ?? ??? ????TreeView?? ??? ???? ????????? ???? ??? ???????? ??? ??, ?? ?? ???? ??? ????????? ???? ?? ??? ???????? ??????? ?? ?????? ?? ??????? ???? ???????

?? ???????????? ???, ?? XML ???? ?? ???? ?? ??? ?? ????????? ???? ?? ??? ?? ????? ????????? ???? ????? ??? ?? ???? ?? ??? ?? ?????? ??? ?? ????, ???? ??????????? Stylesheet ???? (XSL) ???????? ?? ?????? ??, ?? ??? XPath ?????? ?? ??????? ?? ?????? ??? ???? ?????? ????? ?? ??? ??? XML ???????? ????? ?? ???? ??? ???? ?? ??? ??? ???????? ????? ?? ??? ???? ?? ????? ???? ??TreeView???????? ???

?????? ?? ???, ????? ????? ?? ???? XPath ?????? ?? ????? ?? ???? ?????? ???? originial XML ???? ??????? ????, ?? ???? ??? ?? ???? ?? ??? ??? ??? ??? ?? ??? ??? ?????TreeView.
  1. ???? before ??? ?? ????? ???????TreeView1.ExpandAll????? ????? ??? ??????:
                // SECTION 4. Create a new TreeView Node with only the child nodes.
                XmlNodeList nodelist = dom.SelectNodes("//child");
                XmlDocument cDom = new XmlDocument();
                cDom.LoadXml("<children></children>");
                foreach(XmlNode node in nodelist)
                {
                   XmlNode newElem = cDom.CreateNode(XmlNodeType.Element, node.Name, node.LocalName);
                   newElem.InnerText = node.InnerText;
                   cDom.DocumentElement.AppendChild(newElem);
                }
             
                treeView1.Nodes.Add(new TreeNode(cDom.DocumentElement.Name));
                tNode = treeView1.Nodes[1];
                AddNode(cDom.DocumentElement, tNode);
    					
  2. ?????, ?? ??? ????????? ?????? ?? ????????? ??? ?? ??? "??????" ??? ??? ?? ????????? ???? ?????TreeView??? ???? ?? ??????

??????

???? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
313651.NET Framework ??? XML ?? ??? roadmap
???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ?????? ?? ????::
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.XML .aspx

HTTP://msdn2.Microsoft.com/en-us/XML/default.aspx

???

???? ID: 317597 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • 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
??????: 
kbbcl kbctrl kbhowtomaster kbwindowsforms kbmt KB317597 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:317597

??????????? ???

 

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