How to traverse any XML
Editor to share with you how to traverse any XML, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
For arbitrary XML traversal
Class test {PRivate static string root; public static void showxml (string path) {XmlDocument xd = new XmlDocument (); xd.Load (path); XmlNodeList xnl = xd.DocumentElement.ChildNodes; root = xd.FirstChild.NextSibling.Name / / record the root node Console.Write (root+ "\ n"); foreach (XmlNode xn in xnl) {/ / Console.Write (xn.Attributes ["name"] .Value.ToString () + "\ n"); XmlNode child = xn.FirstChild; NodeOperate (child) }} public static void NodeOperate (XmlNode xn1) {if (xn1.HasChildNodes = = true) {Console.Write (xn1.Name + "\ n"); Console.Write ("\ n"); XmlNode childNode = xn1.FirstChild NodeOperate (childNode);} else {Console.Write (xn1.Name + "\ n"); Console.Write (xn1.InnerText); Console.Write ("\ n") If (xn1.NextSibling! = null) {NodeOperate (xn1.NextSibling);} else {int flag = 0 While (xn1.NextSibling = = null) {if (xn1.Name = = root) / / check if the root node is reached If you do not check, there will be a node reference error {flag = 1 Break;} else {xn1 = xn1.ParentNode }} if (flag = = 0) {NodeOperate (xn1.NextSibling) } else if (flag==1) {Console.Write ("End") }} public static void Main () {test.showXML (@ "C:\ Documents and Settings\ SKY\ My Documents\ Visual Studio 2005\ Projects\ Project1\ Project1\ system.xml"); Console.Read () } these are all the contents of the article "how to traverse any XML". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!