直接上代码了,多说无意了。
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);
}
}
}