大家好,又见面了,我是你们的朋友全栈君。
解析文件内容:
<?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);
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158781.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...