时间 : 17-03-23 栏目 : Net开发 作者 : 冰镇宝贝321 评论 : 1 点击 : 2,774 次
解析文件内容:
<?xml version="1.0" encoding="utf-8" ?> <users> <user id="1001"> <name>张三</name> <email>123456789@136.com</email> <phone>13400000000</phone> </user> <user id="1002"> <name>李四</name> <email>123456789@126.com</email> <phone>15800000000</phone> </user> </users>
解析代码:
static void ReadParseXml() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("E:/Data/VisualStudio/C#/app001/ConsoleApp/App01/userlist.xml"); //查找<users> XmlNode root = xmlDoc.SelectSingleNode("users"); //获取到所有<users>的子节点 XmlNodeList nodeList =root.ChildNodes; //遍历所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; XmlNodeList subList = xe.ChildNodes; foreach (XmlNode xmlNode in subList) { if ("name".Equals(xmlNode.Name)) { Console.WriteLine("姓名:" + xmlNode.InnerText); } else if ("email".Equals(xmlNode.Name)) { Console.WriteLine("邮箱:" + xmlNode.InnerText); } } } } static void ReadParseXml2() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("E:/Data/VisualStudio/C#/app001/ConsoleApp/App01/userlist.xml"); //查找<users> XmlNode root = xmlDoc.SelectSingleNode("users"); //获取到所有<users>的子节点 XmlNodeList nodeList = xmlDoc.SelectSingleNode("users").ChildNodes; //遍历所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; Console.WriteLine("节点的ID为: " + xe.GetAttribute("id")); XmlNodeList subList = xe.ChildNodes; foreach (XmlNode xmlNode in subList) { Console.WriteLine(xmlNode.InnerText); } } }
除非注明,文章均为( 冰镇宝贝321 )原创,转载请保留链接: https://bkqv5.com/archives/258.html
很不错,收藏