C# XML 序列化不工作

C# XML Serialization Not working

我有以下代码,它应该将 XML 字符串反序列化为 Class,其中包含 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 ""; }
    }
}   

XML响应也如下:

<?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" />

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

向您的每个属性添加 [XmlAttribute] 属性,它将起作用。

顺便说一句,throw ex 将转储堆栈跟踪,这通常不是您想要的 - 请参阅 this question 了解更多详细信息。如果您不打算对异常做任何事情,完全删除 try/catch 会更好。

解决了,也缺少 class 中的装饰到 Map 中。 [XmlAttribute()] public 字符串 RequestType { 得到;放; }

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