zoukankan      html  css  js  c++  java
  • XML文件注意问题

    一、Elements和Descendant

      Elements 相当于root节点下的子节点,Desendant元素相当于root节点下的所有子节点(包括root.elments下个子节点也包括root.elments节点)。

     1  XDocument xdoc = new XDocument(new XElement("stuList"));
     2             xdoc.Root.Add(new XElement("Stu",new XAttribute("id",1),
     3                                         new XElement("name","张亮"),
     4                                         new XElement("age",21)),                                     
     5                                        new XElement( "Stu",new XAttribute("id",1),
     6                                         new XElement("name","张丽"),
     7                                         new XElement("age",23)),
     8                                        new XElement( "Stu",new XAttribute("id",1),
     9                                         new XElement("name","张飞"),
    10                                        new XElement("age",24))
    11                                         );
    12 
    13             xdoc.Save("1.xml");

           可以看得出生成的XML文件,节点Stu是XML文档根节点stuList.XElements()返回的元素,而stuList.Descendant()返回的是各个Stu和各个Stu节点下的name,age节点。

    二、Name和localName

            我们给根节点StuList加个命名空间,然后进行递归遍历查找xml中的节点如下图所示:

     1   public static void SearchElementsZhao(XElement ele, List<XElement> list)
     2         { 
     3             // 首先遍历ele的所有子节点
     4             foreach (XElement item in ele.Elements())
     5             {
     6                 // 判断这个元素的名字是不是name,如果是name看里面存储的是“赵晓虎”
     7                 if (item.Name.LocalName == "name")
     8                 {
     9                     if (item.Value == "张飞")
    10                     { 
    11                         // 是我要的
    12                         list.Add(item.Parent);
    13                     }
    14                 }
    15 
    16                 // 如果item里面还有子节点就递归
    17                 SearchElementsZhao(item, list);
    18             }

     就会发现item.Name返回的不只是item的Name,还返回了根节点的命名空间,而item.Name.localName返回的则是纯碎的item的名字。

  • 相关阅读:
    【WCF】无废话WCF入门教程
    【IIS8】在IIS8添加WCF服务支持
    iOS 中如何将View设置为圆角的矩形?
    在iOS开发中使用FMDB
    iOS中FMDB的使用【单例】
    普通分页笔记
    基础BaseDao
    连接池技术 实现的四个要素:jdbc.properties配置- 读取配置的单例类 --ConfigManage--BaseDao写法
    反射生成对象,调用对象方法
    context分页
  • 原文地址:https://www.cnblogs.com/fuGuy/p/5661340.html
Copyright © 2011-2022 走看看