Home  >  Article  >  Backend Development  >  XML (3) XDocument and XmlDocument recursively read xml files

XML (3) XDocument and XmlDocument recursively read xml files

黄舟
黄舟Original
2017-02-10 16:31:462453browse

Read the xml file into the TreeView through XDocument and XmlDocument, which mainly recursively loads the elements under the xml into the TreeView control.

XDocument

Steps:

(1) Load xml file

(2) Get the root node

(3)Load the xml root element to the TreeView root node

(4) Recursively load elements below the root element (create a method here)


<span style="font-size:18px;">//1、读取xml文件(XDocument)
            //加载xml文件
            XDocument document = XDocument.Load("list1.xml");
            //2、先获取根节点
            XElement rootElement = document.Root;
            //3、将xml的根元素加载到TreeView的根节点上
            TreeNode rootNode = treeView1.Nodes.Add(rootElement.Name.ToString());
            //4、递归加载
            LoadXmlToTreeView(rootElement, rootNode.Nodes);</span>


<span style="font-size:18px;"> private void LoadXmlToTreeView(XElement rootElement, TreeNodeCollection treeNodeCollection)
        {
            //获取根元素rootElement下面的所有直接子元素
            //rootElement.Elements();
            foreach (XElement item in rootElement.Elements())
            {
                if (item.Elements().Count()==0)
                {

                    treeNodeCollection.Add(item.Name.ToString ()).Nodes .Add(item.Value);
                }
                else
                {
                    //将当前子元素加到TreeView的节点集合上
                    TreeNode node = treeNodeCollection.Add(item.Name.ToString());
                    LoadXmlToTreeView(item, node.Nodes);
                }
                

            }
        }</span>


XmlDocument

Steps: The first three steps of XmlDocument are similar to those of XDocument. The difference is the fourth step. Recursive loading can be seen mainly from the code.


<span style="font-size:18px;">//1、加载xml文件到对象
            XmlDocument document = new XmlDocument();
            //2、将xml文件加载到dom对象上
            document.Load("List1.xml");
            //3、获得xml根节点
            XmlElement rootElement = document.DocumentElement;
            //将xml根元素加载到TreeView上
            TreeNode rootnode = treeView1.Nodes.Add(rootElement.Name);
            //实现递归将xml文件加载到treeview上
            LoadxmltoTreeViews(rootElement, rootnode.Nodes);</span>
<span style="font-size:18px;">private void LoadxmltoTreeViews(XmlElement rootElement, TreeNodeCollection treeNodeCollection)
        {

            //循环rootElement下的所有子元素加载到TreeNodeCollection集合中
            foreach (XmlNode item in rootElement.ChildNodes)
            {
                //在继续之前需要判断一下当前节点是什么类型的节点
                if (item.NodeType ==XmlNodeType .Element )
                {
                    //如果当前节点是一个元素,则把该元素加载到TreeView上
                    TreeNode node= treeNodeCollection.Add(item.Name);
                    //递归调用
                    LoadxmltoTreeViews((XmlElement)item, node.Nodes);

                }
                else if (item.NodeType ==XmlNodeType .Text |item.NodeType ==XmlNodeType .CDATA)
                {
                    treeNodeCollection.Add(item.InnerText);

                }
            }
        }</span>


##Summary

(1) XmlDocument is more complicated than XDocument.

(2) XmlDocument is a standard xml reading and writing class, so it has a wide range of extensions. XDocument is an upgraded version of XmlDocument and may not be used on other platforms, because these platforms may The original XmlDocument is used, but some methods or properties of XDocument do not exist here. So there are certain limitations.

(3) The syntactic sugar var in the foreach loop can identify the type for XDocument, but not for XmlDocument, but the parent class of XmlElement's parent class is XmlNode.


The above is XML (3) XDocument and XmlDocument recursively read the content of the xml file. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn