B. Youngmman February 2016

Deserializing xml to list object returning null

I'm trying to deserialize xml data using xmlreader into a list object but I am getting a null back from my call. Here is a sample of my xml data...

<ExceptionLog>
    <ExceptionLogData MessageCount="1" SourceDateTime="2016-02-08T09:32:41.713" MinSourceDateTime="2016-02-08T09:32:41.713" DataId="610029" MaxExceptionLogID="610029" MessageText="INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session hash: hX7K7LONeTilw5RfGT432g==&#xA;This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. " MachineName="VERTEXDPORTSQL1" AppDomainName="VTMS.Windows.SalesforceServicingAgent.exe" ProcessName="VTMS.Windows.SalesforceServicingAgent" />
    <ExceptionLogData MessageCount="1" SourceDateTime="2016-02-08T09:22:39.340" MinSourceDateTime="2016-02-08T09:22:39.340" DataId="610028" MaxExceptionLogID="610028" MessageText="INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session hash: rtZTrLk2f99iVttLoz31tg==&#xA;This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. " MachineName="VERTEXDPORTSQL1" AppDomainName="VTMS.Windows.SalesforceServicingAgent.exe" ProcessName="VTMS.Windows.SalesforceServicingAgent" />
</ExceptionLog>

This is the object class code that I am trying to create...

public class ExceptionLog {
    public ExceptionLog() {
        ExceptionLogData = new List<ExceptionLogExceptionLogData>();
    }

    public List<ExceptionLogExceptionLogData> ExceptionLogData { get; set; }
}

public class ExceptionLogExceptionLogData {
    private DateTime _sourceDateTimeField;

    private DateTime _minSourceDateTimeField;

    private uint dataIdField;

    private uint _maxExceptionLogIdField;

    priva        

Answers


Volkan Paksoy February 2016

XmlSerializer is defined of type ExceptionLog but you're then casting the result to List

var serializer = new XmlSerializer(typeof(ExceptionLog));
var returnDataList = serializer.Deserialize(dataReader) as List<ExceptionLogExceptionLogData>;

The casting should be to the type of serializer:

var returnDataList = serializer.Deserialize(dataReader) as ExceptionLog;

I didn't check all the elements but you should also mark ExceptionLogData with XmlElement attribute.

[XmlElement]
public List<ExceptionLogExceptionLogData> ExceptionLogData { get; set; }

There might be some issues with the other properties but this should address the problem in the question

Post Status

Asked in February 2016
Viewed 3,208 times
Voted 9
Answered 1 times

Search




Leave an answer