紧急:解决IXmlSerializable和嵌套XmlSerializer冲突 [英] Urgent: Resolve IXmlSerializable and nested XmlSerializer conflict

查看:72
本文介绍了紧急:解决IXmlSerializable和嵌套XmlSerializer冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以

这是一些奇怪的行为,我需要能够快速解决它.

this is some odd behavior and I need to be able to resolve it quickly.

    [Serializable]
    public class BaseClass //: CustomSerializer <-- Ignore for now
    {
        public string Name { get; set; }
        public int Age { get; set; }
        [XmlAttribute("gndr")]
        public char Gender { get; set; }
        [XmlAttribute]
        public Guid ID { get; set; }
    }

    [Serializable]
    [XmlRoot("BaseClass")]
    public class DaughterClass : BaseClass
    {
        public string Value { get; set; }
        public bool? Car { get; set; }
        public int[] Stuff { get; set; }
        public BaseClass Brother { get; set; }
    }

    [Serializable]
    [XmlRoot("BaseClass")]
    public class SonClass : BaseClass
    {
        public string Value { get; set; }
        public bool Bike { get; set; }
        public byte[] Data { get; set; }
        public BaseClass[] Sisters { get; set; }
    }

对继承的类进行简单的模拟.现在,我正在尝试实现IXmlSerializable,以便主要根据对象的Property类型而不是xsi:type属性来推断类型.我有我的原因,所以请不要给我1000的原因 无关.

Simple mock up of inherited classes.  Now, I am trying to implement the IXmlSerializable to primarily infer types based upon the Object's Property type NOT the xsi:type attribute.  I have my reasons, so please do not innundate me with a 1000 why's that are irrelevant

您可以看到所有三个类都导出到"BaseClass"中.元素节点.是的,如果有必要,我确实涉及一些属性类型输入,但是这个想法太过防止XmlSerializer超出xsi:type属性,因此我可以处理 序列化过程.到目前为止,我之前已经做过,这还不是问题.

You can see all three classes export to the "BaseClass" element node.  And yes I do have some attribute typing involved if necessary, but the idea is too prevent the XmlSerializer from blowing up over the xsi:type attribute so I can handle the serialization process.  thus far, i've done this before and it hasn't been an issue.

但是,在实现IXmlSerializable的过程中,我有:

However, in my implementation of IXmlSerializable I have:

        private void Serialize(XmlWriter writer, PropInfo prop, bool arrayElem, object value)
        {
            Type itemType = value != null ? value.GetType() : null;
            XmlSerializer ser = this.GetSerializer(null, prop, arrayElem, ref itemType);

            if (ser == null)
            {
                if (!arrayElem && prop.IsArray) this.SerializeArray(writer, prop, itemType, value);
                else this.SerializePrimitive(writer, prop, itemType, value);
            }
            else
            {
                string element = this.GetElementName(prop, itemType);
                if (value != null)
                {
                    writer.WriteStartElement(element);  /* PROBLEM */
                    ser.Serialize(writer, value);
                    writer.WriteEndElement();
                }
                else writer.WriteElementString(element, null);
            }
        }

现在,该方法正在使用反射,从而确定属性是否需要XmlSerializer,例如DaughterClass.Brother属性的类型为BaseClass,因此它需要嵌套"属性. XmlSerializer.但是,与默认处理不同 关于序列化的信息,当我使用这种方法时,我得到了XmlSerializer属性的双精度元素.

Now this method is using reflection and such to determine if a property needs a XmlSerializer like for example the DaughterClass.Brother property is of type BaseClass so it needs a "nested" XmlSerializer.  Yet, unlike the default handling of the serialization, when I use this method I get a double element for XmlSerializer properties.

示例:

<!-- This is What XmlSerializer produces without any IXmlSerializable implementation -->
<?xml version="1.0"?>
<BaseClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" gndr="70" ID="8a9925ef-e70d-4267-b922-cc7849f6bd70">
  <Name>Viola</Name>
  <Age>22</Age>
  <Value>Sugar N' Spice</Value>
  <Car>true</Car>
  <Stuff>
    <int>3</int>
    <int>13</int>
    <int>5</int>
    <int>10</int>
  </Stuff>
  <Brother xsi:type="SonClass" gndr="77" ID="d53e0437-a4c9-47c8-9fb7-582bc0c46a87">
    <Name>Victor</Name>
    <Age>24</Age>
    <Value>Slugs N' Snails</Value>
    <Bike>false</Bike>
    <Data>Hzoo0hz+NKDGvwvLVlU/fifcCUL8KK40VzXY3xNBX5uM0bROTgYYBi3GlNXUj2nI6OUtr7JLsPaL83rnJVSMhWBwPoWg8TAhwwMUNpPZX8jLEPcuE6/oQSC9wlqLMeHZOQ91ZFDkJ/a0DOyvNnlispSyGxRdXnGwh/xk4y/WlpEhO/2DZPp45+kAeQbCOAqnWxDG2RbMfNuexNxTf0TH3nZbjvt83k2ZOTMrXORq7k7yvYceEL17VL8i5aAeJGikOZqQZ6oPKnzCLJ/ioR/2M9aOYcK8qM643zk7WfmtexXLhiJK2Yak4pILW+MdmLOfLLHKj8Kl6JDL14dx1NCDFQ==</Data>
    <Sisters>
      <BaseClass xsi:type="DaughterClass" gndr="70" ID="ea7c8ecb-8e39-41ff-a6d3-18b557dd678b">
        <Name>Viola</Name>
        <Age>22</Age>
        <Value>Sugar N' Spice</Value>
        <Car xsi:nil="true" />
        <Stuff>
          <int>3</int>
          <int>13</int>
          <int>5</int>
          <int>10</int>
        </Stuff>
      </BaseClass>
      <BaseClass gndr="77" ID="7e87292f-6eaf-4343-a044-1de6e9ed8cfe">
        <Name>Alfred</Name>
        <Age>56</Age>
      </BaseClass>
    </Sisters>
  </Brother>
</BaseClass>


<!-- This is what my CustomSerializer - IXmlSerializable Implementation produces -->
<?xml version="1.0"?>
<BaseClass custom="DaughterClass" gndr="70" ID="7cc04003-3b4f-4d61-aafc-6914dabf9fa9">
  <Value>Sugar N' Spice</Value>
  <Car>true</Car>
  <Stuff>
    <int>3</int>
    <int>13</int>
    <int>5</int>
    <int>10</int>
  </Stuff>
  <Brother>
    <BaseClass custom="SonClass" gndr="77" ID="3620d6b9-587f-4421-b041-2567253d2b4d">
      <Value>Slugs N' Snails</Value>
      <Bike>false</Bike>
      <Data>QlyNnQC3qrtUvIyJSecJibIy11ak0njFGQC7CoMTMP8cIZH9gZjlFTQeZ39OQaP3X8ILm7fVnXDkx4QA/J84QIuNbCdVDAcZTMEhhmv4d9dcw6rBB5VHR4MhGLoT5RmU5ib0wNR8olO68k72sxcF7E+sozj1djj1RSSjM/NNBWanjJ9EA7YD3yFXBhVGUZRsHCxqZkHhn2/t/nuv2W5gbOzoTiZ3wOiYqcMXnnAjrvSQNhfG454kxuwwVwQ7Uway9pkpXgJt9FxE0b19yQH/ylCHH6JXmolNw4g5R2njXVa9ZafW+lDxIkK7ghHhpvzrMY20szNkWlwQ3Jp6NK3yYA==</Data>
      <Sisters>
        <BaseClass>
          <BaseClass custom="DaughterClass" gndr="70" ID="bdec131b-4525-4e13-ab95-49ffda3c155b">
            <Value>Sugar N' Spice</Value>
            <Car />
            <Stuff>
              <int>3</int>
              <int>13</int>
              <int>5</int>
              <int>10</int>
            </Stuff>
            <Brother />
            <Name>Viola</Name>
            <Age>22</Age>
          </BaseClass>
        </BaseClass>
        <BaseClass>
          <BaseClass gndr="77" ID="fc244cd6-f7c7-4135-a886-fff4dd78b734">
            <Name>Alfred</Name>
            <Age>56</Age>
          </BaseClass>
        </BaseClass>
      </Sisters>
      <Name>Victor</Name>
      <Age>24</Age>
    </BaseClass>
  </Brother>
  <Name>Viola</Name>
  <Age>22</Age>
</BaseClass>

如您所见,从DaughterClass.Brother属性为typeof(SonClass)创建的XmlSerializer,以及从BaseClass属性的SonClass.Sisters数组为typeof(DaughterClass)创建的XmlSerializer都会注入根" ;元素 它要序列化的类.我需要防止这种情况,但是我无法控制IXmlSerializable. Root元素是在我的代码到达之前由XmlSerializer编写的.

As you can see, the XmlSerializer that is created for typeof(SonClass) from the DaughterClass.Brother property, as well as the XmlSerializer for typeof(DaughterClass) from the SonClass.Sisters array of BaseClass property, injects the "root" element of the class it's serializing.  I need to prevent this but I have no control inside the IXmlSerializable.  The Root element is written by the XmlSerializer before my code gets to it.

有解决方案吗?

谢谢

Jaeden"Sifo Dyas" al'Raec Ruiner

Jaeden "Sifo Dyas" al'Raec Ruiner



永远不要信任计算机.您的大脑比任何微芯片都要聪明."
PS-不要在其他人的问题上标记答案.诸如假期和假期之类的事情可能会减少及时的活动,并且直到提出问题的人能够测试您的答案之前,仅因为您认为正确,这是不正确的.标记它 为他们正确设置通常会阻止其他人阅读问题,甚至可能无法提供真正的正确"配置.答案.

"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.

推荐答案

嗨JaedenRuiner,

Hi JaedenRuiner,

根据您的描述,建议您可以使用DataContractSerializer代替XmlSerializer.

According to your description, I suggest you can use DataContractSerializer to instead XmlSerializer.

因为,DataContractSerializer比XmlSerializer快.它显式地显示出哪个

Because,DataContractSerializer is faster than XmlSerializer.And it explicitly shows the which

字段或属性被序列化为XML.DataContractSerializer不需要任何默认值

fields or properties are serialized into XML .DataContractSerializer does not need any default

构造函数,然后再序列化任何类型.但是XmlSerializer只是序列化公共财产.

constructor before serializing any type. But the XmlSerializer just Serializes  public properties.

希望对您有所帮助.

最好的问候,

Tracy Dj


这篇关于紧急:解决IXmlSerializable和嵌套XmlSerializer冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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