xml转json c语言,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 { get; set; }

public AllTest() { }

}

public class Test

{

[XmlAttribute]

public string id;

[XmlElement]

public List TestDy { get; set; }

public Test() { }

public Test(string id)

{

this.id = id;

}

}

public class TestDy : DynamicObject, IDictionary, ICloneable, INotifyPropertyChanged

{

IDictionary _values = new Dictionary();

#region IDictionary 接口实现

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 Keys

{

get { return _values.Keys; }

}

public ICollection Values

{

get { return _values.Values; }

}

public void Add(KeyValuePair item)

{

_values.Add(item);

}

public void Add(string key, object value)

{

_values.Add(key, value);

}

public void Clear()

{

_values.Clear();

}

public bool Contains(KeyValuePair item)

{

return _values.Contains(item);

}

public bool ContainsKey(string key)

{

return _values.ContainsKey(key);

}

public void CopyTo(KeyValuePair[] array, int arrayIndex)

{

_values.CopyTo(array, arrayIndex);

}

public IEnumerator> GetEnumerator()

{

return _values.GetEnumerator();

}

public bool Remove(KeyValuePair 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;

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

///

/// 获取属性值

///

///

///

public object GetPropertyValue(string propertyName)

{

if (_values.ContainsKey(propertyName) == true)

{

return _values[propertyName];

}

return null;

}

///

/// 设置属性值

///

///

///

public void SetPropertyValue(string propertyName, object value)

{

if (_values.ContainsKey(propertyName) == true)

{

_values[propertyName] = value;

}

else

{

_values.Add(propertyName, value);

}

}

///

/// 实现动态对象属性成员访问的方法,得到返回指定属性的值

///

///

///

///

public override bool TryGetMember(GetMemberBinder binder, out object result)

{

result = GetPropertyValue(binder.Name);

return result != null;

}

///

/// 实现动态对象属性值设置的方法。

///

///

///

///

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();

alltest.Test.Add(new Test("1"));

alltest.Test[0].TestDy = new List();

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值