c# XML序列化不起作用

本文关键字:不起作用 序列化 XML | 更新日期: 2023-09-27 18:02:51

我有下面的代码,它应该反序列化XML字符串到一个类,在XML文件中有大多数项目,它可能没有所有的,这是好的,他们应该只是假设NULL。

当我使用下面的XML运行以下代码时,它将每个值保留为NULL。

我哪里做错了

感谢
ServiceResponse ReturnVal = new ServiceResponse();
try
{
    XmlSerializer serializer = new XmlSerializer(typeof(ServiceResponse));
    StringReader sr = new StringReader(XMLResponse);
    NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr);
    ReturnVal = (ServiceResponse)serializer.Deserialize(XMLWithoutNamespace);
}
catch (Exception ex)
{
    throw ex;
}

[XmlRoot("ServiceResponse")]
public class ServiceResponse
{
    public string RequestType { get; set; }
    public string ApplicationSender { get; set; }
    public string WorkstationID { get; set; }
    public string POPID { get; set; }
    public string RequestID { get; set; }
    public string ReferenceNumber { get; set; }
    public string ProtocolVersion { get; set; }
    public string DeviceType { get; set; }
    public string SWChecksum { get; set; }
    public string CommunicationProtocOl { get; set; }
    public string Model { get; set; }
    public string ApplicationSoftwareVersion { get; set; }
    public string Manufacturer_Id { get; set; }
    public string OverallResult { get; set; }
}
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
    public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }
    public override string NamespaceURI
    {
        get { return ""; }
    }
}   

XMLResponse将等于以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><ServiceResponse RequestType="Login" ApplicationSender="QWERTY" WorkstationID="1" RequestID="1254" ProtocolVersion="12" DeviceType="1" SWChecksum="1" CommunicationProtocol="11" Model="1" ApplicatioSoftwareVersion="010" Manufacturer_Id="0" OverallResult="Success" xmlns="http://www.nrf-arts.org/IXRetail/namespace" xmlns:IFSF="http://www.ifsf.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nrf-arts.org/IXRetail/namespace C:'Schema'ServiceResponse.xsd" />

c# XML序列化不起作用

您的所有属性都是属性XmlSerializer将把它们解释为元素,除非您另有说明。

[XmlAttribute]属性添加到每个属性中,它将工作。

作为题外话,throw ex将转储堆栈跟踪,这通常不是你想要的-看到这个问题的进一步细节。如果您不打算对异常做任何事情,则完全删除try/catch会更好。

算出来了,被遗漏的装饰在班级里也要映射进去。[XmlAttribute ()]公共字符串请求类型{获取;设置;}

    [XmlAttribute()]
    public string ApplicationSender { get; set; }
    [XmlAttribute()]
    public string WorkstationID { get; set; }
    [XmlAttribute()]
    public string POPID { get; set; }
    [XmlAttribute()]
    public string RequestID { get; set; }
    [XmlAttribute()]
    public string ReferenceNumber { get; set; }
    [XmlAttribute()]
    public string ProtocolVersion { get; set; }
    [XmlAttribute()]
    public string DeviceType { get; set; }
    [XmlAttribute()]
    public string SWChecksum { get; set; }
    [XmlAttribute()]
    public string CommunicationProtocOl { get; set; }
    [XmlAttribute()]
    public string Model { get; set; }
    [XmlAttribute()]
    public string ApplicationSoftwareVersion { get; set; }
    [XmlAttribute()]
    public string Manufacturer_Id { get; set; }
    [XmlAttribute()]
    public string OverallResult { get; set; }