zoukankan      html  css  js  c++  java
  • 以字典形式生成xml

    层级字典构造:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace test1
    {
        public class ToDic
        {
            public ToDic(Annotation annotation)
            {
                _annotation = annotation;
            }
            public Annotation _annotation;
            public class Annotation
            {
                public string _folder;
                public string _filename;
                public string _path;
                public Source _source;
                public Size _size;
                public string _segmented;
                //public List<_Object> _objects { get; set; }
                public List<_Object> _objects = new List<_Object>();
    
                public Annotation(string folder,
                    string filename,
                    string path,
                    Source source,
                    Size size,
                    string segmented,
                    List<_Object> objects)//构造器
                {
                    _folder = folder;
                    _filename = filename;
                    _path = path;
                    _source = source;
                    _size = size;
                    _segmented = segmented;
                    for (int i = 0; i < objects.Count; i++)
                    {
                        _objects.Add(objects[i]);
                    }
                }
                
                public class Source
                {
                    public string _database;
    
                    public Source(string database)//构造器
                    {
                        _database = database;
                    }
                }
    
                public class Size
                {
                    public string _width;
                    public string _height;
                    public string _depth;
    
                    public Size(string width,
                        string height,
                        string depth)//构造器
                    {
                        _width = width;
                        _height = height;
                        _depth = depth;
                    }
                }
    
                public class _Object
                {
                    public string _name;
                    public string _pose;
                    public string _truncated;
                    public string _difficult;
                    public Bndbox _bndbox;
    
                    public _Object(string name, 
                        string pose,
                        string truncated,
                        string difficult,
                        Bndbox bndbox)//构造器
                    {
                        _name = name;
                        _pose = pose;
                        _truncated = truncated;
                        _difficult = difficult;
                        _bndbox = bndbox;
                    }
    
    
                    public class Bndbox
                    {
                        public string _xmin;
                        public string _ymin;
                        public string _xmax;
                        public string _ymax;
    
                        public Bndbox(string xmin,
                            string ymin,
                            string xmax,
                            string ymax)//构造器
                        {
                            _xmin = xmin;
                            _ymin = ymin;
                            _xmax = xmax;
                            _ymax = ymax;
                        }
                    }
                }
            }
    
            
    
            public static Dictionary<string, object> ToDictionary(object o)
            {
                if (o == null)
                {
                    return null;
                }
                var fileds = o.GetType().GetFields();
                Dictionary<string,object> dic = new Dictionary<string, object>();
                
                foreach (var field in fileds)
                {
                    if (field != null)
                    {
                        if (field.FieldType == typeof(List<Annotation._Object>))
                        {
                            int i = 0;
                            foreach (var v in (field.GetValue(o) as List<Annotation._Object>))//遍历object列表
                            {
                                dic.Add((field.Name+i).Split('_')[1], ToDictionary(v));//将列表名+i作为字典的键名,将列表元素的值递归加入字典
                                i++;
                            }
                        }
                        else if (field.FieldType == typeof(string))
                        {
                            dic.Add(field.Name.Split('_')[1], field.GetValue(o));
    //                        foreach (var kvp in dic)
    //                        {
    //                            Console.WriteLine($"{kvp.Key}:{kvp.Value}");
    //                        }
                        }
                        else
                        {
                            dic.Add(field.Name.Split('_')[1], ToDictionary(field.GetValue(o)));
                        }
                    }
                }
                return dic;
            }
            
        }
    }
  • 相关阅读:
    bzoj 2742(树状数组)
    [网络流24题(3/24)] 最长k可重区间集问题(洛谷P3358)
    bzoj 1087(状压dp)
    算法模板整理V1.0
    ACM资料汇总
    算法笔记
    NC20861 兔子的逆序对(数学基础)
    zzuli新生周赛第四周题解
    Gym 102028E Resistors in Parallel(大数)
    HDU 3974 Assign the task(dfs序建线段树)
  • 原文地址:https://www.cnblogs.com/Manuel/p/13489144.html
Copyright © 2011-2022 走看看