using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Dynamic; namespace DynamicReadXml
{
public static class ExpandoXML
{
public static dynamic AsExpando(this XDocument xDocument)
{
return CreateExpando(xDocument.Root);
} private static dynamic CreateExpando(XElement element)
{
var result = new ExpandoObject() as IDictionary<string, object>;
if (element.Elements().Any(e => e.HasElements))
{
var list = new List<ExpandoObject>();
result.Add(element.Name.ToString(), list);
foreach (var childElement in element.Elements())
{
list.Add(CreateExpando(childElement));
}
}
else
{
foreach (var leafElement in element.Elements())
{
result.Add(leafElement.Name.ToString(), leafElement.Value);
}
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq; namespace DynamicReadXml
{
class Program
{
static void Main(string[] args)
{
var doc = XDocument.Load("employee.xml");
var result = doc.AsExpando();
foreach (var employee in result.Employees)
{
Console.WriteLine(employee.FirstName);
}
Console.ReadKey();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<FirstName>DebugLZQ1</FirstName>
</Employee>
<Employee>
<FirstName>DebugLZQ2</FirstName>
</Employee>
<Employee>
<FirstName>DebugLZQ3</FirstName>
</Employee>
<Employee>
<FirstName>DebugLZQ4</FirstName>
</Employee>
<Employee>
<FirstName>DebugLZQ5</FirstName>
</Employee>
<Employee>
<FirstName>DebugLZQ6</FirstName>
</Employee>
</Employees>

.net4 dynamic parse xml的更多相关文章

  1. Parse xml/json[xpath/jpath]

    import groovy.util.XmlSlurper import groovy.util.XmlParser import com.eviware.soapui.support.GroovyU ...

  2. [Android] Android Build 时报错: java.io.IOException: Could not parse XML from android/accounts/annotations.xml

    Android构建时报错: app:lintVitalRelease[Fatal Error] :3:214: 与元素类型 “item” 相关联的 “name” 属性值不能包含 ‘<’ 字符. ...

  3. python parse xml using DOM

    demo: import xml.dom.minidom dom=xml.dom.minidom.parse('sample.xml')root = dom.documentElementcc=dom ...

  4. 【RF库XML测试】parse xml

    Name:Parse XmlSource:XML <test library>Arguments:[ source | keep_clark_notation=False ]Parses ...

  5. 递归方式 DOM 解析(parse) XML

    friends.xml <span style="font-size:16px;"><?xml version="1.0" encoding= ...

  6. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

  7. parse XML & js

    parse XML & js how to parse xml data in js? https://stackoverflow.com/questions/17604071/parse-x ...

  8. idea报错。Error:Failed to load project configuration: cannot parse xml file E:\project\.idea\workspace.xml: Error on line 1: 前言中不允许有内容。

    因为电脑卡死强制重启电脑后打开idea,进行junit单元测试报错: idea报错.Error:Failed to load project configuration: cannot parse x ...

  9. (C#) Parse xml 时, 返回的node值总是null。

    网上查了一下,原因在于要parse的Xml文件本身包含了一些namespace,这些需要被添加进去. http://msdn.microsoft.com/zh-cn/library/system.xm ...

随机推荐

  1. ORACLE启动 切换实例命令

    启动服务器的其他实例 export ORACLE_SID=数据库实例名 sqlplus /nolog conn /as sysdba select name from v$database; !lsn ...

  2. Hibernate3的jar包

    一.hibernate3包说明 说明: Hibernate 软件包中的Hibernate3.jar 是我们需要使用的Hibernate 工具,其他引用的 Jar 文件位于lib 子目录下,Hibern ...

  3. mongodb多表查询(附带pymongo实例)

    mongodb有$lookup可以做多表查询 举个例子 数据如下 db.orders.insert([ { , , }, { , , }, { } ]) db.inventory.insert([ { ...

  4. Efficient GPU Screen-Space Ray Tracing

    http://jcgt.org/published/0003/04/04/paper.pdf 一个号称只有2ms的实时gpu光线追踪 screen space reflection用到了 和其他ray ...

  5. 一个手绘normal的方法

    https://polycount.com/discussion/98983/how-to-paint-flow-anisotropic-comb-maps-in-photoshop flow map ...

  6. 2017.12.25 Mybatis物理分页插件PageHelper的使用(二)

    参考来自: 官方文档的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 上篇博客 ...

  7. OpenGL(八)使用 subroutine 切换可编程管线

    Subroutine 功能是在OpenGL 4.0 版本号里才添加的.因此对于各种Android手机.这个功能基本跪了.假设你发现你的程序报错:ARB_shader_subroutine.那就说明当前 ...

  8. 标准C++ I/O库 迭代器让数据自由流动 V8

    IO库的组成 三种流 C++的IO库以流对象为实体.主要有三种流: (1)标准输入输出流 (2)文件输入输出流 (3)字符串输入输出流 流迭代器 输入输出流迭代器 instream_iterator& ...

  9. 菜单下拉效果demo记录

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. HDU 1253 胜利大逃亡 NYOJ 523【BFS】

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...