用于内部对象奇怪错误的 Xml 序列化程序 [英] Xml serializer for inner object strange bug

查看:21
本文介绍了用于内部对象奇怪错误的 Xml 序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSD 架构,包括一些简单"部分和更多复杂"部分(它们不能使用框架属性以正确的方式序列化).通过在内部对象上实现 IXmlSerializable 接口,我将 .Net 序列化程序用于简单部分,并为更复杂的部分编写了自定义序列化程序.

I have an XSD schema including some "simple" parts and more "complex" parts (they can not be serialized the proper way using the framework attributes ). I use the .Net serializer for the simple parts and wrote a custom serializer for the more complex parts, by implementing the IXmlSerializable interface on the inner object.

当我测试代码时,我只输出反序列化的自定义"部分(读取时).如果我在根"类上注释引用复杂对象的属性,那么所有简单的序列化都会发生(读和写).看起来手工序列化器接管了序列化的所有控制,而不是根据需要仅序列化内部对象.在我看来这是一个奇怪的行为,那么错误在哪里??

When I test the code, I get on the output only the "custom" part deserialized (when reading). If I comment the property referencing the complex object on the "root" class, then all the simple serialization happens (read & write). It seams that the handmade serializer takes all the control over the serialization instead of serializing only the inner object as wanted. Up to me this is a strange behavior, so where is the bug??

是否可以仅在内部对象上使用 IXmlSerializable?

Is it possible to use the IXmlSerializable on an inner object only?

这是根"类:

public class RootElement
{
    [XmlAttribute("foo")]
    public Foo foo;

    [XmlAttribute("bar")]
    public Bar? bar;

    public bool ShouldSerializeBar()
    {
        return bar.ShouldSerialize;
    }

    [XmlElement("SimpleXml")]
    public SimpleXml simpleXml;

    // commenting these two lines radically change the serialization
    [XmlElement("ComplexXmlWithCustomSerializer")]
    public ComplexXml complexXml;
}

结束这是ComplexXml"类

End this is the "ComplexXml" class

 public class ComplexXml : IXmlSerializable
{
    public double pty1;

    public double? pty2;

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        XmlReader reader2 = reader.ReadSubtree();
        while (reader2.Read())
        {
            if (reader2.NodeType == XmlNodeType.Element)
            {
                string unit;
                switch (reader2.Name)
                {
                    case "Pty1":
                        unit = reader2.GetAttribute("unit");
                        if (string.Equals(unit, "mm"))
                            pty1 = double.Parse(reader2.GetAttribute("value"));
                        break;
                    case "Pty2":
                        unit = reader2.GetAttribute("unit");
                        if (string.Equals(unit, "deg"))
                            pty2 = double.Parse(reader2.GetAttribute("value"));
                        break;
                }
            }
            if (reader2.NodeType == XmlNodeType.EndElement)
                reader2.ReadEndElement();
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        //pty1
        writer.WriteStartElement("Pty1");
        writer.WriteAttributeString("unit", "mm");
        writer.WriteAttributeString("value", pty1.ToString());
        writer.WriteEndElement();

        //pty2
        if (pty2.HasValue)
        {
            writer.WriteStartElement("Pty2");
            writer.WriteAttributeString("unit", "deg");
            writer.WriteAttributeString("value", WrapAngle.Value.ToString());
            writer.WriteEndElement();
        }
    }
}

推荐答案

我可以在一个类似的问题中找到这个问题的答案 此处.

I could find an answer to this problem in a similar question here.

重点是用

            reader.Read();

我不明白为什么它会起作用,但是这一行使读者能够在自定义方法结束后继续阅读 XML 文档...

I do not understand precisely why it works, but this line enables the reader to continue the reading of the XML document after the end of the custom method...

这篇关于用于内部对象奇怪错误的 Xml 序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆