创建XmlSerializer正确序列化/反序列化派生类型 [英] Creating XmlSerializer that serializes/deserializes derived types correctly

查看:115
本文介绍了创建XmlSerializer正确序列化/反序列化派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个XmlSerializer,以正确地序列化和反序列化派生类型.请看下面的代码.非常感谢您在使用XmlAttributeOverrides广告附加类型来创建适当的XmlSerializer以及将具有VeteerObject的GetVehicleResponse实例序列化为"SUV"对象方面的帮助.

I'm trying to create an XmlSerializer that serialize and deserializes derived types properly. Please take a look at the code below. Any assistance in using XmlAttributeOverrides ad extra types to create proper XmlSerializer and serialize an instance of GetVehicleResponse with VehicleObject as "SUV" object is greatly appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using InteractiveSoftworks.Framework.Xml;
using System.IO;

namespace DowncastTest
{
   [XmlType(Namespace="urn:Test/Service")]
   public class GetVehicleResponse
   {
      [XmlElement(IsNullable=true, Namespace="urn:Test")]
      public Vehicle VehicleObject;
   }

   [XmlType( Namespace = "urn:test" )]
   public class Vehicle
   {
      public string Model;
      public string Number { get; set; }
   }

   public class Car : Vehicle
   {
      public int Doors { get; set; }
   }

   public class SUV : Car
   {
      public int Engines { get; set; }
   }

   public class MotorCycle : Vehicle
   {
      public int Seats { get; set; }
   }

   public class SportsBike : MotorCycle
   {
      public int Mirrors { get; set; }
   }


   public class Program
   {
      static void Main( string[] args )
      {
         XmlAttributeOverrides overrides = new XmlAttributeOverrides();
         CreateAttributeOverrides( typeof( Car ), "urn:Test", overrides );
         CreateAttributeOverrides( typeof( SUV ), "urn:Test", overrides );
         CreateAttributeOverrides( typeof( MotorCycle ), "urn:Test", overrides );
         CreateAttributeOverrides( typeof( SportsBike ), "urn:Test", overrides );

         Type[] extraTypes = new Type[] { typeof( Car ), typeof( SUV ), typeof( MotorCycle ), typeof( SportsBike ) };

         XmlSerializer xs = new XmlSerializer( typeof( GetVehicleResponse ), overrides, extraTypes, new XmlRootAttribute() { ElementName = "GetVehicleResponse", Namespace = "urn:Test" }, "urn:Test" );

         MemoryStream ms = new MemoryStream();
         xs.Serialize( ms, new GetVehicleResponse() { VehicleObject = new SUV() { Number = "AP29", Model = "2011", Doors = 4, Engines = 2 } } );

         string s = Encoding.UTF8.GetString( ms.GetBuffer() );

         Console.WriteLine( s );

         Console.WriteLine( "Done..." );
         Console.ReadKey();
      }

      internal static void CreateAttributeOverrides( Type type, string projectNamespace, XmlAttributeOverrides overrides )
      {
         // redirect the type if no explicit XmlAttributeType namespace has been provided
         //
         XmlAttributes typeAttributes = new XmlAttributes( type );
         XmlTypeAttribute typeAttribute = null;

         if ( typeAttributes.XmlType != null ) // inherit existing methodType attributes if any
         {
            if ( string.IsNullOrEmpty(typeAttributes.XmlType.Namespace) ) // only set the namespace if it isn't already defined
            {
               typeAttribute = typeAttributes.XmlType;
               typeAttribute.Namespace = projectNamespace;
            }
         }
         else
         {
            string rootNamespace = string.Empty;

            // if type defined Xml Root Attributes then get the namespace and add to type attributes
            //
            if ( typeAttributes.XmlRoot != null )
               rootNamespace = typeAttributes.XmlRoot.Namespace;

            if ( string.IsNullOrEmpty( rootNamespace ) )
               rootNamespace = projectNamespace;

            typeAttribute = new XmlTypeAttribute() { Namespace = rootNamespace };

         }

         if ( typeAttribute != null )
            overrides.Add( type, new XmlAttributes() { XmlType = typeAttribute } );  // use a fresh XmlAttributes as we only want to globally override XmlTypeAttribute
      }

   }
}

推荐答案

作为一种替代方法,您可以使用

As an alternative approach, you can use a form of the XmlSerializer that supports an extra types parameter that you can use to specify derived types: From the doc:

您还可以使用extraTypes参数指定从基类派生的类型.

You can also use the extraTypes parameter to specify types derived from a base class.

根据我的经验,这很好,但是由于您需要准备好解决Microsoft实施中的潜在问题,因此这并不是每种情况的完美选择.请参见 XmlSerializer文档中的动态生成的程序集"部分. a>详细信息.

This works fine in my experience, but is not perfect for every situation since you need to be prepared to address potential issues in Microsoft's implementation. See the section on Dynamically Generated Assemblies in the XmlSerializer doc for details.

这篇关于创建XmlSerializer正确序列化/反序列化派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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