Xml文件解析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XmlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();  //得到一个处理xml的对象
            doc.Load("user.xml");  //把xml文件加载进来

            XmlNode root = doc.DocumentElement;  //获得根节点

            Console.WriteLine("节点名称是:" + root.Name);

            foreach (XmlNode item in root.ChildNodes)  //得到根节点下面的所有子节点
            {
                foreach (XmlNode citem in item.ChildNodes)
                {

                    switch (citem.Name)
                    {
                        case "id":
                            Console.WriteLine("学生的编号是:" + citem.InnerText);
                            break;
                        case "name":
                            Console.WriteLine("学生的姓名是:" + citem.InnerText);
                            break;
                        case "age":
                            Console.WriteLine("学生的年龄是:" + citem.InnerText);
                            break;
                    }
                }
            }

            XmlDocument doc = new XmlDocument();
            doc.Load("FullChannels.xml");
            //获取所有xml内容,第一种方法     
            XmlNode items = doc.DocumentElement;
            foreach (XmlNode item in items.ChildNodes)
            {          
                foreach (XmlNode citem in item.ChildNodes)
                {

                    switch (citem.Name)
                    {
                        case "channelType":
                            Console.WriteLine("电视类型:" + citem.InnerText);
                            break;
                        case "tvChannel":
                            Console.WriteLine("电视台:" + citem.InnerText);
                            break;
                        case "path":
                            Console.WriteLine("电视台路径:" + citem.InnerText);
                            break;
                    }
                }
            }
            

            //第二种方法获取元素
            XmlElement xmlRoot = doc.DocumentElement;       
            foreach (XmlNode item in xmlRoot.ChildNodes)
            {
                Console.WriteLine(item["channelType"].InnerText + "=\t" + item["tvChannel"].InnerText + "=\t" + item["path"].InnerText);
            }
            //第三种方法,获取节点集合
            XmlNodeList list = doc.GetElementsByTagName("channelType");
            foreach (XmlNode item in list)
            {
                Console.WriteLine(item.InnerText);
            }
            
            Console.ReadLine();



        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值