zoukankan      html  css  js  c++  java
  • linq xml 转

    本文介绍如何使用 Descendants、Elements快速遍历XML节点

    首先准备一个简单但是常见的XML
    复制  保存<?xml version="1.0" encoding="utf-8" ?>
    <userSet>
      <userInfo id="1" name="Guozhijian">
        <profile>
          <phoneNumber>13818181818</phoneNumber>
          <country>China</country>
        </profile>
      </userInfo>
      <userInfo id="2" name="Zhenglanzhen">
        <profile>
          <phoneNumber>13919191919</phoneNumber>
          <country>Korea</country>
        </profile>
      </userInfo>
    </userSet><?xml version="1.0" encoding="utf-8" ?>
    <userSet>
      <userInfo id="1" name="Guozhijian">
        <profile>
          <phoneNumber>13818181818</phoneNumber>
          <country>China</country>
        </profile>
      </userInfo>
      <userInfo id="2" name="Zhenglanzhen">
        <profile>
          <phoneNumber>13919191919</phoneNumber>
          <country>Korea</country>
        </profile>
      </userInfo>
    </userSet>
    测试一:
    复制  保存private void Test1()
    {
        XDocument xdoc = XDocument.Load(@"UserSet.xml");
        var users = from u in xdoc.Descendants("userInfo")
                    where u.Attribute("id").Value == "1"
                    select u;
        foreach (var u in users)
        {
            string name = u.Attribute("name").Value;
            Console.WriteLine(name);
        }
    }
    输出结果为:
    Guozhijian

    测试二
    复制  保存private void Test2()
    {
        XDocument xdoc = XDocument.Load(@"UserSet.xml");
        var users = from u in xdoc.Root.Elements("userInfo")
                    where u.Element("profile").Element("phoneNumber").Value == "13919191919"
                    select u;
        foreach (var u in users)
        {
            string name = u.Attribute("name").Value;
            Console.WriteLine(name);
        }
    }
    输出结果为:
    Zhenglanzhen

  • 相关阅读:
    mysqldump备份数据库时排除某些库
    Dataguard后台进程解析
    mysql 查看所有存储过程
    Oracle 中的 TO_DATE 和 TO_CHAR 函数
    trunc的使用
    mysql now() sysdate() curdate()区别
    ORA-10456:cannot open standby database;media recovery session may be in process
    ORACLE 修改日志大小及增加日志成员
    Oracle 11gR2用gpnp profile存放ASM的spfile路径
    C语言malloc()函数:动态分配内存空间
  • 原文地址:https://www.cnblogs.com/eebb/p/1166208.html
Copyright © 2011-2022 走看看