【问题标题】:Deserializing xml to list object returning null反序列化 xml 以列出返回 null 的对象
【发布时间】:2016-02-08 17:03:36
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: c# xml xml-deserialization


    【解决方案1】:

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

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

    【讨论】:

    • 做到了!非常感谢。

      我还进去并将 [XmlAttribute] 添加到我所有的公共属性中,现在一切正常。
    • @B.Youngmman:太好了,我很高兴它对你有用。如果它有助于解决您的问题,您也可以接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多