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

  • 相关阅读:
    [转]java 常用弹出框
    [转]ImageIcon icon 相对路径设置
    [转]『基本ASCII表和c语言运算表查询』
    [转]sqlmap技术手册
    [转]linux下怎么查看ssh的用户登录日志
    [转]Kali-linux安装之后的简单设置
    查看任意程序所连接的ip地址
    JS 闭包
    JS 中的 继承
    JS 原型的 理解
  • 原文地址:https://www.cnblogs.com/eebb/p/1166208.html
Copyright © 2011-2022 走看看