如何 XML 将 ExpandoObject 序列化为 property/value 对?

How to XML serialize an ExpandoObject into property/value pairs?

我在调用 OnActionExecuted 时拦截 Web API 2 管道。在这里,我将操作返回的对象递归地转换为 ExpandoObject(即对象上本身是对象的任何属性也会转换为 ExpandoObject,依此类推层次结构)。

它 XML 可以序列化,但只能作为字典(大概是因为 ExpandoObject 实现了 IDictionary,它只是从中提取键和值)。我宁愿看到它序列化,就好像它是一个具有属性的对象,而不是一堆 key/value 对。

有什么方法可以在不编写我自己的 XML 序列化器的情况下做到这一点?

您可以将 ExpandoObject 包装在 ISerializable 实现中。它递归包装包含 ExpandoObject.

[Serializable]
public class SerializableWrapper : ISerializable
{
    private IDictionary<string, object> _data;

    public IDictionary<string, object> Data
    {
        get { return _data; }
    }

    public SerializableWrapper(IDictionary<string, object> data)
    {
        _data = data;
    }

    protected SerializableWrapper(SerializationInfo info, StreamingContext context)
    {
        this._data = new Dictionary<string, object>();
        var enumerator = info.GetEnumerator();
        while (enumerator.MoveNext())
        {
            this._data[enumerator.Name] = enumerator.Value;
        }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {            
        foreach (var kvp in this._data)
        {
            info.AddValue(kvp.Key, Wrap(kvp.Value));
        }
    }

    private static object Wrap(object value)
    {
        var expando = value as ExpandoObject;
        if (expando != null)
        {
            return new SerializableWrapper(expando);
        }
        var expandoList = value as IEnumerable<ExpandoObject>;
        if (expandoList != null)
        {
            return expandoList
                .Select(Wrap)
                .Cast<SerializableWrapper>()
                .ToArray();
        }
        var list = value as IEnumerable;
        if (list != null && !(value is string))
        {
            return list
                .Cast<object>()
                .Select(Wrap)
                .ToArray();
        }
        return value;
    }
}
dynamic obj = new ExpandoObject();
obj.Foo = 3;
obj.Bar = new [] { new ExpandoObject() };
obj.Bar[0].Baz = "Qux";

var wrapped = new SerializableWrapper(obj);

var ser = new DataContractSerializer(typeof(SerializableWrapper), new [] { typeof(SerializableWrapper[]), typeof(object[]) });
var mem = new MemoryStream();
ser.WriteObject(mem, wrapped);

生成:

<SerializableWrapper xmlns="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema">
  <Foo i:type="x:int" xmlns="">3</Foo>
  <Bar i:type="a:ArrayOfSerializableWrapper" xmlns="" xmlns:a="http://schemas.datacontract.org/2004/07/">
    <a:SerializableWrapper>
      <Baz i:type="x:string">Qux</Baz>
    </a:SerializableWrapper>
  </Bar>
</SerializableWrapper>

连载的XML不好看。您可以使用 DataContractResolver 或 post 处理 XML 以使其不那么难看。

要再次反序列化,可以使用

mem.Position = 0;
var deserialized = (SerializableWrapper) ser.ReadObject(mem);

另一种方法是实施 IXmlSerializable 并改用 XmlSerializer