.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>
时间: 2024-10-12 16:19:47

.net4 dynamic parse xml的相关文章

How to parse Xml file -- DOM!

We can see that when we want to use the Dom4j to parse XML , we should import org.dom4j.* At the same time ,that to parse XML by DOM ,we should import org.w3c.dom.* The conclusion is that DOM and Dom4j is different and located in different jar.The DO

递归方式 DOM 解析(parse) XML

friends.xml [html] view plain copy <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> <friends date="2012"> <!--此处为注释--> <friend id="1"> <姓名>小红</姓名&

How to parse Xml file -- SAX!

Different from DOM parser, the SAX parser will parse the file from one node to another. There are several methods are common used in SAX parser: startDocument() startElement() character() endElement() endDocument() For example: <Books> ------> st

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

Android构建时报错: app:lintVitalRelease[Fatal Error] :3:214: 与元素类型 “item” 相关联的 “name” 属性值不能包含 ‘<’ 字符. Could not read /Users/panxin/Library/Android/sdk/platform-tools/api/annotations.zip java.io.IOException: Could not parse XML from android/accounts/annota

parse xml document using dom method

the bookstore.xml: <?xml version="1.0" encoding="UTF-8"?><bookstore>    <book id="1">        <bookname>weicheng</bookname>        <author>qianzhongshu</author>    </book>    <

A powerful tool to parse xml -- Xpath!

The Xpath's javadoc could be got from the Dom4j's javadoc. There are some simple regulation you must follow: 1.When you want to use specific single node: /AAA /AAA/CCC 2.When you want to use all the nodes which have the same name: //BBB //DDD/CCC 3.

不一样的dynamic解析json

写过javascript的人都知道js解析json 1:(JSON) 字符串转换为对象. var str = '{"name":"lsw","hobby":"free"}'; 1.1:JSON.parse(str); 2:对象解析出字符串 var a = {a:1,b:2}; JSON.stringify(a) 3:注意:浏览器兼容问题 网上有json2.js文件 或者jquery文件 问题来了c# 写过webapi 或者we

教你在你的应用程序中扩展使用dynamic类型

教你在你的应用程序中扩展使用dynamic类型 相信大家在开发中经常会接触到mvc中的ViewBag,有心的同学会,发现这就是一个dynamic类型,查看源码一谈究竟,本文也是根据dynamic来扩展一些实际中的应用,方便开发过程中使用,来提高大家的工作效率等.这里我给大家介绍一个简单的,解析xml的扩展类. 先新建一个类,DynamicXmlNode 继承自DynamicObject,然后我们输入override,看一下,都有哪些方法可以重写的: 看到其中有2个方法,感觉很接近我们要用的,我这

【收藏用】--切勿转载Java处理XML的三种主流技术及介绍

原帖地址 : http://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Markup Language) 意为可扩展标记语言,它已经是软件开发行业中大多数程序员和厂商用以选择作为数据传输的载体.本文作者对于 Java 处理 XML 的几种主流技术进行一些总结和介绍,希望帮助那些有不同需求的开发人员对于 XML 处理技术的作出最优的选择. 最初,XML 语言仅仅是意图用来作为 HTML 语言的替代品而出现的,但是随着该