static void Main(string[] args)
{
XmlTextReader reader;
if (File.Exists("student.xml"))//如果文件已存在,载入文档
{
reader = new XmlTextReader("student.xml");
}
else//否则
{
return;
}
int count = 0;
while (reader.Read())//阅读下一个
{
if (reader.Name == "student")
{
//显示读取的属性和中间文本
Console.WriteLine(reader.GetAttribute("id") + " " + reader.GetAttribute("age") + " " + reader.ReadString());
count++;
}
}
reader.Close();//关闭阅读器
Console.WriteLine("Count is " + count);
Console.ReadKey();
}
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
if (File.Exists("student.xml"))//如果文件已存在,载入文档
{
doc.Load("student.xml");
}
else//否则
{
Console.WriteLine("文档不存在!");
Console.ReadKey();
return;
}
XmlNodeList list = doc.DocumentElement.SelectNodes("student");//读取根节点的所有子节点,放到XmlNodeList中
foreach (XmlNode node in list)//从list中遍历所有节点
{
XmlElement ele = (XmlElement)node;//节点可以有中间文本但是没有属性值,所以要先转成element才能读出属性值
Console.WriteLine(ele.GetAttribute("id") + " " + ele.GetAttribute("age") + " " + ele.InnerText);//读取数据/显示
}
Console.ReadKey();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有