C#对象序列化到XML并忽略null元素

时间:2018-07-20 10:06:24

标签: c# xml xml-serialization xmlserializer

[XmlRoot("SAPInformationInterchangeXML")]
public class EWayBillResponseXML
{
    [XmlElement(ElementName = "SAPBusinessNetworkCustomerID")]
    public string SAPBusinessNetworkCustomerID { get; set; }

    [XmlElement(ElementName = "INVOIC")]
    public ResponseINVOIC Invoice { get; set; }
}

public class ResponseINVOIC
{
    [XmlElement(ElementName = "HeaderInformation")]
    public string HeaderInformation { get; set; }

    [XmlElement(ElementName = "AuthorizationInformation")]
    public string AuthorizationInformation { get; set; }
}


    var encoding = Encoding.GetEncoding("ISO-8859-1");
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = false,
        Encoding = encoding
    };

    string requestHeaderInformation = null, requestAuthorizationInformation = null;

    EWayBillResponseXML obj = new EWayBillResponseXML
                    {
                        SAPBusinessNetworkCustomerID = "1",
                        Invoice = new ResponseINVOIC
                        {
                            HeaderInformation = requestHeaderInformation,
                            AuthorizationInformation = requestAuthorizationInformation
                        }
    };

    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(obj.GetType());

    using (var stream = new MemoryStream())
    {
        using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
        {
            x.Serialize(xmlWriter, obj);
        }
        Console.WriteLine(encoding.GetString(stream.ToArray()));
    }

我已经创建了两个名为EWayBillResponseXML和ResponseINVOIC的对象。我尝试使用上述代码片段进行序列化。它为我提供了序列化的XML,但它也返回了null对象元素。我不需要序列化XML中的null对象。你能帮我吗?

当前正在输出:

<?xml version="1.0" encoding="iso-8859-1"?>
<SAPInformationInterchangeXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SAPBusinessNetworkCustomerID>1</SAPBusinessNetworkCustomerID>
<INVOIC />
</SAPInformationInterchangeXML>

预期输出:

<?xml version="1.0" encoding="iso-8859-1"?>
<SAPInformationInterchangeXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SAPBusinessNetworkCustomerID>1</SAPBusinessNetworkCustomerID>
</SAPInformationInterchangeXML>

2 个答案:

答案 0 :(得分:0)

您可以将以下方法添加到可序列化的类(EWayBillResponseXML)中:

        public bool ShouldSerializeInvoice()
        {
            return Invoice != null;
        }

您可以read more about that here

答案 1 :(得分:0)

正如@gmiley所说,您应该使方法命名为ShouldSerialize___,其中___是应该序列化或不应该序列化的属性的名称。该方法必须在称为属性的类中

因此,在您的情况下,请在您的ShouldSerializeResponseINVOIC类中创建EWayBillResponseXML方法

[XmlRoot("SAPInformationInterchangeXML")]
public class EWayBillResponseXML
{
    [XmlElement(ElementName = "SAPBusinessNetworkCustomerID")]
    public string SAPBusinessNetworkCustomerID { get; set; }

    [XmlElement(ElementName = "INVOIC")]
    public ResponseINVOIC Invoice { get; set; }

    public book ShouldSerializeInvoice()
    {
        return Invoice != null;
    }

}

编辑: dbc在评论中正确地说,发票实际上不是空的而是空的,因此您应该处理该问题。