1
[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()));
    }

I have created 2 objects named EWayBillResponseXML and ResponseINVOIC. I tried to serialize using above code snippet. It gives me serialized XML but it returns null object element too. I don't need null object in serialized XML. Can you Please help me out.

Currently getting output:

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

Expected output:

<?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>
1
  • 2
    <INVOIC /> isn't a null element. A null element would look like <INVOIC xsi:nil="true" />. Instead, it's an empty element. It is emitted because the property Invoice is non-null and allocated, however all of its members are null so no child elements are created. If you want to prevent empty elements from being emitted for non-null members, see How do I stop and empty tag in XML serializer?. In fact this may be a duplicate. Agree?
    – dbc
    Jul 20, 2018 at 20:22

2 Answers 2

0

You can add the following method to your serializable class (EWayBillResponseXML):

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

You can read more about that here.

3
  • 1
    above snippet not working
    – Ronak
    Jul 20, 2018 at 10:19
  • It works for me. I use this extensively in production code that I wrote a year or two ago. Not to mention, you can see the example of this on Microsoft's site. Did you put it in the same class as the property is declared? How are you using it, and what is "not working" about it?
    – gmiley
    Jul 20, 2018 at 10:23
  • This does not solve @Ronak's problem because, in their code, Invoice is actually non-null. All its members are null, however, which is why an empty element is emitted for it. See dotnetfiddle.net/MKmfDL
    – dbc
    Jul 20, 2018 at 22:04
0

As @gmiley said, you should make method named like this ShouldSerialize___ where ___ is the name of property that should or not be serialized. That method needs to be in class where is said property

So, in your case, make ShouldSerializeResponseINVOIC method in your EWayBillResponseXML class

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

}

Edit: Dbc correctly said in comment that Invoice is not actually null but empty, so you should handle that.

2
  • This does not solve @Ronak's problem because, in their code, Invoice is actually non-null. All its members are null, however, which is why an empty element is emitted for it. Furthermore, in any ShouldSerializeXXX() method the name XXX must match the property name, here Invoice. In your code you are using the name of the returned type ResponseINVOIC which is not correct. See dotnetfiddle.net/A20c5A
    – dbc
    Jul 20, 2018 at 22:07
  • You are right in both cases. I saw you comment on question and saw that you're right. At least I'll fix the code to actually compiles and returns if property should be serialized :)
    – Nino
    Jul 21, 2018 at 5:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.