zoukankan      html  css  js  c++  java
  • .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>
  • 相关阅读:
    cnn softmax regression bp求导
    使用kd-tree加速k-means
    KDTree详解及java实现
    加入商品分类信息,考虑用户所处阶段的 图模型 推荐算法 Rws(random walk with stage)
    用户标签
    LDA(latent dirichlet allocation)
    对物品进行反馈 代码
    1.虚拟机中安装ubuntu
    4.动态HTML处理和机器图像识别
    3.非结构化数据与结构化数据提取
  • 原文地址:https://www.cnblogs.com/asingna/p/5386164.html
Copyright © 2011-2022 走看看