• XmlReader 使用


    <Root>
      <Phone Name="Oppo手机">
        <BackGround Description="颜色">白色 黑色 玫瑰色 金色</BackGround>
        <Price Description="价格">2000</Price>
      </Phone>
      <Phone Name="Vivo手机">
        <BackGround Description="颜色">白色 黑色 </BackGround>
        <Price Description="价格">1500</Price>
      </Phone>
      <Phone Name="小米手机">
        <BackGround Description="颜色">白色 黑色 </BackGround>
        <Price Description="价格">1800</Price>
      </Phone>
      <Phone Name="锤子手机">
        <BackGround Description="颜色">银色 黑色 酒红 金色 </BackGround>
        <Price Description="价格">2200</Price>
      </Phone>
    </Root>
      public void ReaderXml()
            {
                try
                {
                    //声明StringReader传入Xml文本,作为XmlReader.Create的参数
                    using (StreamReader strReader = new StreamReader(xmlPath))
                    {
                        //通过XmlReader.Create静态方法创建XmlReader实例
                        using (XmlReader item = XmlReader.Create(strReader))
                        {
                            //循环Read方法直到文档结束
                            while (item.Read())
                            {
    
                                //如果不是xml元素则跳过
                                if (item.NodeType != XmlNodeType.Element) continue;
    
                                //通过item.Name得到节点名
                                string elementName = item.Name;
                                if (elementName == "Root") continue;
    
                                if (elementName == "Phone")
                                {
                                    var name = item.GetAttribute("Name");
                                    Console.WriteLine($"一级节点的属性:{name}");
    
                                    if (item.Read())
                                    {
                                        Console.WriteLine($"一级节点的值:{item.Value}");
                                    }
                                }
                                else if (elementName == "BackGround")
                                {
                                    var name = item.GetAttribute("Description");
                                    Console.WriteLine($"二级节点手机的属性:{name}");
                                    if (item.Read())
                                    {
                                        Console.WriteLine($"二级节点手机的值:{item.Value}");
                                    }
                                }
                                else if (elementName == "Price")
                                {
                                    var name = item.GetAttribute("Description");
                                    Console.WriteLine($"二级节点的属性:{name}");
                                    if (item.Read())
                                    {
                                        Console.WriteLine($"二级节点的值:{item.Value}");
                                    }
                                }
    
                            }
                        }
                    }
    
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
  • 相关阅读:
    pcr的简单小脚本
    Q宠大乐斗自动玩
    Docker安装elsearch
    elasticserarch6.5.0百度云资源
    rabbitmq安装
    排序后分组取每个分组的第一条数据
    Lambda表达式的简单使用
    解决SpringBoot总是找不到jsp
    Java强转类型时报错
    iOS内购图文流程(2017)
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/9115388.html
Copyright © 2020-2023  润新知