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>

最新文章

  1. 使用spring的AOP时产生的异常
  2. 网站性能,javascript性能相关知识点
  3. Ubuntu下使用SVN
  4. 让 ListView 在 Android 可回弹
  5. Activiti系列:是否可以让某些流程的信息写到历史表,而另外一些不写?
  6. windows7设置java的环境变量
  7. java获取类路径
  8. HttpContext.Current.Session=null问题
  9. .net 判断日期格式yyyy-MM-dd hh:MM:ss的正则表达式
  10. C3P0连接池详细配置
  11. windows安装nvm管理node版本
  12. Javascript二(函数详解)
  13. spring微服务架构-脑图
  14. [P1396]营救 (并查集)
  15. JS笔记(二):对象
  16. P1550 [USACO08OCT]打井Watering Hole
  17. 关于IEnumerator&lt;T&gt;泛型枚举器 和 IEnumerable&lt;T&gt;
  18. 技术向:一文读懂卷积神经网络CNN
  19. Nvidia显卡驱动下载
  20. 2016424王启元 Exp2 后门原理与实践

热门文章

  1. java浏览器控件jxbrowser(简单demo模拟自动登录与点击)
  2. HDU 5288 OO’s Sequence 水题
  3. Android Content Provider Security(转)
  4. 让Code First下的数据库的迁移更加简单
  5. GNU Debugger for Windows----GDB
  6. css/js(工作中遇到的问题)-6
  7. 19个三维GIS软件对比
  8. HDU 4886 TIANKENG’s restaurant(Ⅱ) hash+dfs
  9. find . -mtime +1 查找文件
  10. solr 高亮显示