将xml反序列化为列表对象,返回null

时间:2016-02-08 17:03:36

标签: c# xml xml-deserialization

我试图使用xmlreader将xml数据反序列化为列表对象,但是我从调用中得到了一个空值。以下是我的xml数据示例...

<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>

这是我想要创建的对象类代码......

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;

    private string _messageTextField;

    private string _machineNameField;

    private string _appDomainNameField;

    private string _processNameField;

    public byte MessageCount { get; set; }

    public DateTime SourceDateTime {
        get {
            return _sourceDateTimeField;
        }
        set {
            _sourceDateTimeField = value;
        }
    }

    public DateTime MinSourceDateTime {
        get {
            return _minSourceDateTimeField;
        }
        set {
            _minSourceDateTimeField = value;
        }
    }

    public uint DataId {
        get {
            return dataIdField;
        }
        set {
            dataIdField = value;
        }
    }

    public uint MaxExceptionLogID {
        get {
            return _maxExceptionLogIdField;
        }
        set {
            _maxExceptionLogIdField = value;
        }
    }

    public string MessageText {
        get {
            return _messageTextField;
        }
        set {
            _messageTextField = value;
        }
    }

    public string MachineName {
        get {
            return _machineNameField;
        }
        set {
            _machineNameField = value;
        }
    }

    public string AppDomainName {
        get {
            return _appDomainNameField;
        }
        set {
            _appDomainNameField = value;
        }
    }

    public string ProcessName {
        get {
            return _processNameField;
        }
        set {
            _processNameField = value;
        }
    }
}

最后,这是我试图反序列化数据的方式......

        using (var dataReader = sqlCommand.ExecuteXmlReader())
        {
            var serializer = new XmlSerializer(typeof(ExceptionLog));

            var returnDataList = serializer.Deserialize(dataReader) as List<ExceptionLogExceptionLogData>;
            return returnDataList;
        }

我错过了什么或我做错了什么?

我有另一种方法,我可以使用,直到我弄明白,这是创建我的对象列表并以编程方式在运行中用我的对象填充它的老式方法 - 不是非常优雅,但暂时它的工作原理。

TIA

1 个答案:

答案 0 :(得分:0)

XmlSerializer的定义类型为ExceptionLog,但您将结果转换为List

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

转换应该是序列化器的类型:

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

我没有检查所有元素,但您还应该使用XmlElement属性标记ExceptionLogData。

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

其他属性可能存在一些问题,但这应解决问题中的问题