有时候我们需要处理接口返回的值,有时候是json,有时候是xml,如果是xml,就需要用XMLDocument来声明一个对象进行处理
解析文件内容:
张三 123456789@136.com 13400000000 李四 123456789@126.com 15800000000
解析代码:
static void ReadParseXml() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("E:/Data/VisualStudio/C#/app001/ConsoleApp/App01/userlist.xml"); //查找XmlNode root = xmlDoc.SelectSingleNode("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"); //查找 XmlNode root = xmlDoc.SelectSingleNode("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); } } }