zoukankan      html  css  js  c++  java
  • c#动态类转json,再由json转xml

    直接上代码了,多说无意了。

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Dynamic;
    using System.Runtime.CompilerServices;
    using Newtonsoft.Json;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication5
    {
    
        public class AllTest
        {
            [XmlElement]
            public List<Test> Test { get; set; }
            public AllTest() { }
        }
        public class Test
        {
            [XmlAttribute]
            public string id;
            [XmlElement]
            public List<TestDy> TestDy { get; set; }
            public Test() { }
            public Test(string id)
            {
                this.id = id;
            }
        }
        public class TestDy : DynamicObject, IDictionary<string, object>, ICloneable, INotifyPropertyChanged
        {
            IDictionary<string, object> _values = new Dictionary<string, object>();
            #region IDictionary<String, Object> 接口实现
            public object this[string key]
            {
                get { return _values[key]; }
                set
                {
                    _values[key] = value;
                    OnPropertyChanged(key);
                }
            }
            public int Count
            {
                get { return _values.Count; }
            }
            public bool IsReadOnly
            {
                get { return _values.IsReadOnly; }
            }
            public ICollection<string> Keys
            {
                get { return _values.Keys; }
            }
            public ICollection<object> Values
            {
                get { return _values.Values; }
            }
            public void Add(KeyValuePair<string, object> item)
            {
                _values.Add(item);
            }
    
            public void Add(string key, object value)
            {
                _values.Add(key, value);
            }
            public void Clear()
            {
                _values.Clear();
            }
            public bool Contains(KeyValuePair<string, object> item)
            {
                return _values.Contains(item);
            }
            public bool ContainsKey(string key)
            {
                return _values.ContainsKey(key);
            }
            public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
            {
                _values.CopyTo(array, arrayIndex);
            }
            public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
            {
                return _values.GetEnumerator();
            }
            public bool Remove(KeyValuePair<string, object> item)
            {
                return _values.Remove(item);
            }
            public bool Remove(string key)
            {
                return _values.Remove(key);
            }
            public bool TryGetValue(string key, out object value)
            {
                return _values.TryGetValue(key, out value);
            }
            IEnumerator IEnumerable.GetEnumerator()
            {
                return _values.GetEnumerator();
            }
            #endregion
            #region ICloneable 接口实现
            public object Clone()
            {
                var clone = new TestDy() as IDictionary<string, object>;
                foreach (var key in _values.Keys)
                {
                    clone[key] = _values[key] is ICloneable ? ((ICloneable)_values[key]).Clone() : _values[key];
                }
                return clone;
            }
            #endregion
            #region INotifyPropertyChanged 接口实现
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
            /// <summary>  
            /// 获取属性值  
            /// </summary>  
            /// <param name="propertyName"></param>  
            /// <returns></returns>  
            public object GetPropertyValue(string propertyName)
            {
                if (_values.ContainsKey(propertyName) == true)
                {
                    return _values[propertyName];
                }
                return null;
            }
            /// <summary>  
            /// 设置属性值  
            /// </summary>  
            /// <param name="propertyName"></param>  
            /// <param name="value"></param>  
            public void SetPropertyValue(string propertyName, object value)
            {
                if (_values.ContainsKey(propertyName) == true)
                {
                    _values[propertyName] = value;
                }
                else
                {
                    _values.Add(propertyName, value);
                }
            }
            /// <summary>  
            /// 实现动态对象属性成员访问的方法,得到返回指定属性的值  
            /// </summary>  
            /// <param name="binder"></param>  
            /// <param name="result"></param>  
            /// <returns></returns>  
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                result = GetPropertyValue(binder.Name);
                return result != null;
            }
            /// <summary>  
            /// 实现动态对象属性值设置的方法。  
            /// </summary>  
            /// <param name="binder"></param>  
            /// <param name="value"></param>  
            /// <returns></returns>  
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                SetPropertyValue(binder.Name, value);
                return true;
            }      
            public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
            {
                return base.TryInvoke(binder, args, out result);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                AllTest alltest = new AllTest();
                alltest.Test = new List<Test>();
                alltest.Test.Add(new Test("1"));
                alltest.Test[0].TestDy = new List<TestDy>();
                dynamic dy = new TestDy();
                alltest.Test[0].TestDy.Add(dy);
                dy.Sid = "9527";
                dy.a = "123";
                dy.b = "456";
                dy.c = "789";
                //实体类转json
                string jsonString = JsonConvert.SerializeObject(alltest);          
                Console.WriteLine();
                //json转xml
                XmlDocument xml = JsonConvert.DeserializeXmlNode(jsonString);
                string result = xml.OuterXml;
                Console.WriteLine(result);
    
            }
        }
    
    }
    

      

  • 相关阅读:
    谈谈图片上传及canvas压缩的流程
    前端应该懂得初级Web分析指标
    java OPENCV 连通域, Imgproc.findContours 例子,参数说明
    [学习opencv]高斯、中值、均值、双边滤波
    Opencv 图像叠加 添加水印
    帧间提取水印
    opencv mat 转灰度图
    编写一条sql命令,sql删除没有中文的表
    使用JavaCV/OpenCV抓取并存储摄像头图像
    周掌柜
  • 原文地址:https://www.cnblogs.com/zhouyuqiu/p/11271117.html
Copyright © 2011-2022 走看看