.net4 dynamic parse xml
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的更多相关文章
- Parse xml/json[xpath/jpath]
import groovy.util.XmlSlurper import groovy.util.XmlParser import com.eviware.soapui.support.GroovyU ...
- [Android] Android Build 时报错: java.io.IOException: Could not parse XML from android/accounts/annotations.xml
Android构建时报错: app:lintVitalRelease[Fatal Error] :3:214: 与元素类型 “item” 相关联的 “name” 属性值不能包含 ‘<’ 字符. ...
- python parse xml using DOM
demo: import xml.dom.minidom dom=xml.dom.minidom.parse('sample.xml')root = dom.documentElementcc=dom ...
- 【RF库XML测试】parse xml
Name:Parse XmlSource:XML <test library>Arguments:[ source | keep_clark_notation=False ]Parses ...
- 递归方式 DOM 解析(parse) XML
friends.xml <span style="font-size:16px;"><?xml version="1.0" encoding= ...
- parse XML & JSON & js
parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...
- parse XML & js
parse XML & js how to parse xml data in js? https://stackoverflow.com/questions/17604071/parse-x ...
- 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 ...
- (C#) Parse xml 时, 返回的node值总是null。
网上查了一下,原因在于要parse的Xml文件本身包含了一些namespace,这些需要被添加进去. http://msdn.microsoft.com/zh-cn/library/system.xm ...
随机推荐
- Oracle Extended Tracing
Definitions A trace file is a file that contains diagnostic data used to investigate problems. Als ...
- 调用wsdl的接口-用axis
// 创建一个服务(service)调用(call) org.apache.axis.client.Service service = new org.apache.axis.client.Servi ...
- C#之Enum中的Flag
我们知道在默认情况下,第一个枚举数的值为0,后面每个枚举数的值一次加1. enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; 我们也可以用初始值来重写默认值. ...
- [Android Pro] Android的Animation之LayoutAnimation使用方法
用于为一个里面的控件,或者是一个里面的控件设置动画效果,可以在文件中设置,亦可以在代码中设置. 一种直接在XML文件中设置 1. 在res/anim文件夹下新建一个XML文件,名为list_anim ...
- postgres--流复制
配置 master 192.168.2.21 5432 slave 192.168.2.22 5432 目录 /var/lib/pgsql/10/data/ 配置主库 配置用户 psql create ...
- [Todo]Redis & Mysql可以看的书籍
Redis实战(我下的版本是网络版,还有一版是黄健宏翻译的版本,正在找) 高性能Mysql第三版 都在目录: /Users/baidu/Documents/Data/Interview/存储-Nosq ...
- Java笔记16:多线程共享数据
一.Thread实现 public class ThreadDemo4 { publicstaticvoid main(String[] args) { new ThreadTest4().start ...
- 我的自动化测试历程(Selenium+TestNG+Java+ReportNG+Jenkins)
原地址:http://blog.csdn.net/shilinjie_8952/article/details/53380373?locationNum=11&fps=1 测试环境:Java+ ...
- vb.net小试三层架构
在对三层架构有了初步了解后,用vb.net做了一个小的程序,真的很小,仅仅是为了体现一下三层之间机制.下面是我设计的操作界面: 还有程序集和类的分布情况, 接下来是数据的设计,数据库用到的是SQL S ...
- freemarker的list指令小技术归纳
1.问题:当数据超过3位的时候,freemarker会自动用逗号截取,例如2,311 解决方法(一种即可): (1)加.toString(),如:${(data).toString()} (2)加?c ...