使用XmlMessageFormater序列化的消息无法反序列化List<T>成员。

3
我正在使用C#消息序列化时遇到了一些麻烦。
我有一个类,它的构造函数看起来像这样:
public ProposalRequestMessage(int imaNumber, int proposalId, bool trainingFlag, string firstSiteAddress,
                                  bool lastSiteFlag, string lastSiteAddress, int reasonCode,
                                  List<LaneSelection> theLaneSelections)
    {
        ImaNumber = imaNumber;
        ProposalId = proposalId;
        TrainingFlag = trainingFlag;
        FirstSiteAddress = firstSiteAddress;
        LastSiteFlag = lastSiteFlag;
        LastSiteAddress = lastSiteAddress;
        ReasonCode = reasonCode;
        laneSelections = new List<LaneSelection>(theLaneSelections);
    }

该类的lanesSelections成员是System.Collections.Generic.List类型的,其中LaneSelection的格式如下:
public class LaneSelection
{
    public int LaneId { get; set; }
    public SignalAspect AspectCode { get; set; }

    public LaneSelection()
    {
    }
    public LaneSelection(int laneId, SignalAspect aspectCode)
    {
        LaneId = laneId;
        AspectCode = aspectCode;
    }
}  

一个SignalAspect是一个枚举类型。

我会像下面这样发送一个包含此类实例的消息到MSMQ:

System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(queuename);
        queue.Purge();

System.Messaging.Message msg = new System.Messaging.Message(theMessage, new System.Messaging.XmlMessageFormatter());
        queue.Send(msg);

使用一些调试工具,我发现生成的 XML 稍微有点像这样:
<?xml version="1.0"?>
<IvtmMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
<MessageType>ProposalRequest</MessageType>  
<ProposalRequestMessage>    
    <ImaNumber>0</ImaNumber>    
    <ProposalId>2</ProposalId>    
    <TrainingFlag>false</TrainingFlag>    
    <FirstSiteAddress>M25/4690A</FirstSiteAddress>    
    <LastSiteFlag>false</LastSiteFlag>    
    <LastSiteAddress />    
    <ReasonCode>3</ReasonCode>    
    <LaneSelections>      
        <LaneSelection>        
            <LaneId>1</LaneId>        
            <AspectCode>Advisory20</AspectCode>      
        </LaneSelection>    
    </LaneSelections>  
</ProposalRequestMessage>  

我在另一端这样反序列化消息:
Queue = new System.Messaging.MessageQueue(queueName);
Queue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(IvtmMessage) });
Queue.ReceiveCompleted += new System.Messaging.ReceiveCompletedEventHandler(Queue_ReceiveCompleted);
Queue.BeginReceive(new System.TimeSpan(0, 0, 0, 30));
...
System.Messaging.MessageQueue mq = (System.Messaging.MessageQueue)sender;
try
{    
     // End the asynchronous Receive operation.
     System.Messaging.Message m = mq.EndReceive(e.AsyncResult);

     IvtmMessage message = (IvtmMessage)m.Body;
     DecodeMessage(message);
}
catch (System.Messaging.MessageQueueException ex)
{
     string exception = ex.Message;
}
mq.BeginReceive();

return; 

班级中的每个成员都被正确反序列化,除了laneSelections元素。虽然它在XML中明确有值,但在反序列化消息中评估为null实例。

在分离过程中,我尝试向类添加一个List,在构造时用1-5填充它。如果这样序列化是正确的,那么问题就在于LaneSelection类,否则问题就在于序列化List。但是,List没有正确序列化。

有人知道出了什么问题吗?


1
也许尝试使用XmlSerializer属性来装饰你的类 - 特别是用XmlArray来装饰列表(即使它是一个列表!)http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayattribute.aspx?我知道这适用于XmlSerializer,但不确定你正在序列化的方式。 - Jay
请发布 laneSelections 成员的代码。 - Mike the Tike
你读过 http://stackoverflow.com/questions/1474505/problem-deserializing-generic-lists-with-c-sharp-xmlserializer 吗? - Paul Zahra
2个回答

2
很可能你需要在ProposalRequestMessage类中添加[XmlInclude(typeof(LaneSelection))]属性。

1

两件事情:

  1. IvtmMessage声明中有"laneSelections",但序列化的XML是"LaneSelections",其中L大写。Xml序列化/反序列化是区分大小写的,这让我犯了不止一次错误。
  2. 您是否尝试将以下内容添加到IvtmMessage类的LaneSelections属性中,如下所示的代码?

属性定义:

 [XmlArray("LaneSelections"), XmlArrayElement("LaneSelection")]
 public List<LaneSelection> LaneSelections { get; set; }

没有正确的Xml属性在属性和类上,你将把属性/对象的翻译留给序列化程序来解释。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接