本文介绍了在复杂的对象图使用的IXmlSerializable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用自定义XML序列化(<$ C C $> IXmlSerialiable ),上包含的属性与它做的不是的使用自定义的的IXmlSerializable 界面,你怎么指定,在 IXmlSerializable.ReadXml(XmlReader的读者)的方法,要解串器使用普通的反序列化这些子属性?

If using custom XML Serialization (IXmlSerialiable), on a complex object that contains properties with constituent complex objects which do NOT use custom IXmlSerializable interface, how do you specify, in the IXmlSerializable.ReadXml(XmlReader reader) method, that you want the deserializer to use the ordinary deserialization on those child properties?

注:类似这个问题

推荐答案

的IXmlSerializable 是有点乏味来实现,因为它是pretty的多少全有或全无的方式因为你无法选择正常的XML序列化子类型。不过,如果我理解正确的话,你就可以实现你想要通过手动创造什么的XmlSerializer 对于不执行类型的IXmlSerializable

The IXmlSerializable is a bit tedious to implement since it's pretty much an all or nothing approach given that you cannot select child types for normal XML serialization. However, if I understood you correctly you can achieve what you want by manually creating XmlSerializer for the types that do not implement IXmlSerializable.

例如,如果我们开始了两个班,默认不执行的IXmlSerializable 自定义这确实实现了。

For example, if we start with two classes, Default that does not implement IXmlSerializable and Custom which does implement it.

public class Default // Uses default XML Serialization
{
    public int Count { get; set; }
}

public class Custom : IXmlSerializable
{
    public int Count { get; set; }

    public XmlSchema GetSchema() { throw new NotImplementedException(); }

    public void ReadXml(XmlReader reader)
    {
        reader.ReadToDescendant("Count");
        this.Count = reader.ReadElementContentAsInt();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("Custom");
        writer.WriteElementString("Count", this.Count.ToString());
        writer.WriteEndElement();
    }
}

然后我们创建一个三等功有每个previous实例和农具的子的IXmlSerializable 在调用的ReadXml /中WriteXML 方法来支持它,并创建默认XML序列化的另一个孩子。

Then we create a third class Parent that has a child of each of the previous instances and implements IXmlSerializable in a way that calls ReadXml/WriteXml methods for the child that supports it and create default XML serializer for the other child.

public class Parent : IXmlSerializable
{
    public Parent()
    {
        this.Default = new Default { Count = 1 };
        this.Custom = new Custom { Count = 2 };
    }

    public Default Default { get; set; }
    public Custom Custom { get; set; }

    public XmlSchema GetSchema() { throw new NotImplementedException(); }

    public void ReadXml(XmlReader reader)
    {
        reader.ReadToFollowing("Custom");
        this.Custom = new Custom();
        this.Custom.ReadXml(reader);

        reader.ReadToFollowing("Default");
        var serializer = new XmlSerializer(typeof(Default));
        this.Default = (Default)serializer.Deserialize(reader);
    }

    public void WriteXml(XmlWriter writer)
    {
        this.Custom.WriteXml(writer);

        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        new XmlSerializer(typeof(Default)).Serialize(writer, this.Default, ns);
    }
}

为了使例子​​完整,序列化和反序列化一个父的示例程序例如:

static void Main()
{
    var sb = new StringBuilder();
    var serializer = new XmlSerializer(typeof(Parent));

    serializer.Serialize(new StringWriter(sb), new Parent());

    Console.WriteLine(sb);

    var parent = (Parent)serializer.Deserialize(new StringReader(sb.ToString()));

    Console.WriteLine("Parent.Custom.Count: {0}", parent.Custom.Count);
    Console.WriteLine("Parent.Default.Count: {0}", parent.Default.Count);
}

这篇关于在复杂的对象图使用的IXmlSerializable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 08:33