The xml file is read to The TreeView through XDocument and XmlDocument. the elements in the xml file are loaded to the TreeView control recursively. The xml file is read to The TreeView through XDocument and XmlDocument. the elements in the xml file are loaded to the TreeView control recursively.
XDocument
Steps:
(1) load xml files
(2) get the root node
(3) load the xml root element to the TreeView root node.
(4) recursively load elements under the root element (here we create a method)
// 1. read the xml file (XDocument) // load the xml file XDocument document = XDocument. load ("list1.xml"); // 2. obtain the root node XElement rootElement = document. root; // 3. load the xml Root element to the Root node of the TreeView. TreeNode rootNode = treeView1.Nodes. add (rootElement. name. toString (); // 4. recursively load LoadXmlToTreeView (rootElement, rootNode. nodes );
Private void LoadXmlToTreeView (XElement rootElement, TreeNodeCollection treeNodeCollection) {// Obtain all direct sub-elements under the root element rootElement // rootElement. elements (); foreach (XElement item in rootElement. elements () {if (item. elements (). count () = 0) {treeNodeCollection. add (item. name. toString ()). nodes. add (item. value);} else {// add the current child element to the node set of The TreeView, TreeNode node = treeNodeCollection. add (item. name. toString (); LoadXmlToTreeView (item, node. nodes );}}}
XmlDocument
Step: The first three steps of XmlDocument are similar to XDocument. The difference is the recursive loading of the fourth step, which can be seen from the code.
// 1. load the xml file to the object XmlDocument document = new XmlDocument (); // 2. load the xml file to the dom object document. load ("List1.xml"); // 3. obtain the xml root node XmlElement rootElement = document. documentElement; // load the xml root element to the TreeView. TreeNode rootnode = treeView1.Nodes. add (rootElement. name); // recursively load the xml file to the treeview LoadxmltoTreeViews (rootElement, rootnode. nodes );
Private void LoadxmltoTreeViews (XmlElement rootElement, TreeNodeCollection treeNodeCollection) {// all sub-elements under the cyclic rootElement are loaded into the TreeNodeCollection set foreach (XmlNode item in rootElement. childNodes) {// determine the type of the current node if (item. nodeType = XmlNodeType. element) {// if the current node is an Element, load the Element to the TreeView TreeNode node = treeNodeCollection. add (item. name); // recursively call LoadxmltoTreeViews (XmlElement) item, node. nodes);} else if (item. nodeType = XmlNodeType. text | item. nodeType = XmlNodeType. CDATA) {treeNodeCollection. add (item. innerText );}}}
Summary
(1) XmlDocument is more complex than XDocument.
(2) XmlDocument is a standard xml read/write class, so it has a wide range of extensions. an upgraded version of xml document for XDocument may not be available on other platforms, because these platforms may use the original XmlDocument, some methods or attributes of XDocument do not exist here. Therefore, some limitations exist.
(3) the syntax sugar var in the foreach loop can recognize the type of XDocument, but XmlDocument cannot recognize the type, but the parent class of XmlElement is XmlNode.
The above is the XML (3) XDocument and XmlDocument recursively read the content of the xml file. For more information, see The PHP Chinese website (www.php1.cn )!