XMLDeserialize到Object ISO-8859-1

时间:2015-03-23 15:19:14

标签: c# xml xml-deserialization

我正在尝试将从第三方服务返回的以下XML字符串反序列化为C#对象集合,但未成功:

"<?xml version='1.0' encoding='ISO-8859-1'?><DataService><Request ID=\"2461627\" Status=\"Pending\"><TimeOffDate>2015-07-02</TimeOffDate><TimeStart></TimeStart><TimeEnd></TimeEnd><TimeOffHours>8.000</TimeOffHours><TimeOffTypeName>Holiday</TimeOffTypeName><LoginID>SPete</LoginID><Firstname>Stinky</Firstname><Lastname>Pete</Lastname><Comment><![CDATA[]]></Comment></Request><Request>...</Request></DataService>"

这是我的班级定义:

    [XmlRoot]
public sealed class DataServiceModel {
    List<RequestModel> requests;

    [XmlArray("DataService"), XmlArrayItem("Request", Type = typeof(RequestModel))]
    public List<RequestModel> Requests { get { return requests ?? (requests = new List<RequestModel>()); } }

}

public class RequestModel {
    [XmlAttribute("ID")]
    public int Id { get; set; }
    [XmlAttribute("Status")]
    public string Status { get; set; }
    [XmlElement("TimeOffDate")]
    public DateTime RequestDate { get; set; }
    [XmlElement("TimeStart")]
    public DateTime Start { get; set; }
    [XmlElement("TimeEnd")]
    public DateTime End { get; set; }
    [XmlElement("TimeOffHours")]
    public decimal Hours { get; set; }
    [XmlElement("TimeOffTypeName")]
    public string TimeOffType { get; set; }
    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
    [XmlElement("LoginID")]
    public string LoginId { get; set; }
}

我用来反序列化我正在调用的服务返回的代码:

        protected IEnumerable<RequestModel> ProcessResults(byte[] result) {
            var serializer = new XmlSerializer(typeof(DataServiceModel));
            var isoEncoder = Encoding.GetEncoding("ISO-8859-1");                               
            var resultString = isoEncoder.GetString(result);
            using (var xmlReader = new XmlTextReader(new StringReader(resultString))) {
                var dataServiceModel = (DataServiceModel)serializer.Deserialize(xmlReader);
                return dataServiceModel.Requests;
        }
    }

一切正常,直到最后两行代码将XML字符串反序列化为我收到异常消息的对象:“XML文档中存在错误(1,45)。”它位于DataService标记的开头。

完整的异常消息是:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>There is an error in XML document (1, 45).</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>...</StackTrace>
<InnerException>
    <Message>An error has occurred.</Message>
    <ExceptionMessage><DataService xmlns=''> was not expected.</ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace>at  Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDataServiceModel.Read4_DataServiceModel()
    </StackTrace>
</InnerException>

1 个答案:

答案 0 :(得分:0)

事实证明以下内容对我有用:

protected IEnumerable<Request> ProcessResults(byte[] result) {
    var serializer = new XmlSerializer(typeof(Request[]), new XmlRootAttribute("DataService") {Namespace = ""});
    var isoEncoder = Encoding.GetEncoding("ISO-8859-1");
    var resultString = isoEncoder.GetString(result);
    using (var xmlReader = new XmlTextReader(new StringReader(resultString))) {
        var requests = (Request[])serializer.Deserialize(xmlReader);
        return requests;
    }
}

对Model类进行以下修改:

public sealed class Request {
    [XmlAttribute("ID")]
    public int Id { get; set; }
    [XmlAttribute("Status")]
    public string Status { get; set; }
    public DateTime TimeOffDate { get; set; }
    public string TimeStart { get; set; }
    public string TimeEnd { get; set; }
    public decimal TimeOffHours { get; set; }
    public string TimeOffTypeName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string LoginID { get; set; }
    public string Comment { get; set; }
}

这允许我完全从解决方案中删除DataServiceModel类。