Skip to main content
Fixed bug
Source Link
CharlesNRice
  • 4.4k
  • 15
  • 18

A different option is to fix this at the creation of the XmlSerializer. The XmlSerializer constructor can take a parameter for XmlAttributeOverrides

You could create a method to auto add the XmlElementAttribute, if missing, or set the IsNullable to true.

public static XmlAttributeOverrides AddIsNullableTrue(Type type)
{
    var xmlOverrides = new XmlAttributeOverrides();
    foreach (
        var prop in
            type.GetProperties()
                .Where(p => !p.PropertyType.IsValueType || p.PropertyType == typeof (string)))
    {
        var xmlAttributes = new XmlAttributes();
        var attr = prop.GetCustomAttribute<XmlElementAttribute>();
        if (attr != null && attr.IsNullable == false)
        {
            attr.IsNullable = true;
        }
        else
        {
            attr = new XmlElementAttribute() {IsNullable = true};
        }
        xmlAttributes.XmlElements.Add(attr);
        xmlOverrides.Add(type, prop.Name, xmlAttributes);
    }
    return xmlOverrides;
}

Now when you create your XmlSerializer you just need to pass in the XmlAttributeOverrides

XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass), AddIsNullableTrue(typeof(TestClass)));

This example is assuming the class is called TestClass. Now you don't need to populate null fields and fields will be output to XML that are null.

A different option is to fix this at the creation of the XmlSerializer. The XmlSerializer constructor can take a parameter for XmlAttributeOverrides

You could create a method to auto add the XmlElementAttribute, if missing, or set the IsNullable to true.

public static XmlAttributeOverrides AddIsNullableTrue(Type type)
{
    var xmlOverrides = new XmlAttributeOverrides();
    foreach (
        var prop in
            type.GetProperties()
                .Where(p => !p.PropertyType.IsValueType || p.PropertyType == typeof (string)))
    {
        var xmlAttributes = new XmlAttributes();
        var attr = prop.GetCustomAttribute<XmlElementAttribute>();
        if (attr != null && attr.IsNullable == false)
        {
            attr.IsNullable = true;
        }
        else
        {
            attr = new XmlElementAttribute() {IsNullable = true};
        }
        xmlAttributes.XmlElements.Add(attr);
        xmlOverrides.Add(type, prop.Name, xmlAttributes);
    }
    return xmlOverrides;
}

Now when you create your XmlSerializer you just need to pass in the XmlAttributeOverrides

XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass), AddIsNullableTrue(typeof(TestClass)));

This example is assuming the class is called TestClass. Now you don't need to populate null fields and fields will be output to XML that are null.

A different option is to fix this at the creation of the XmlSerializer. The XmlSerializer constructor can take a parameter for XmlAttributeOverrides

You could create a method to auto add the XmlElementAttribute, if missing, or set the IsNullable to true.

public static XmlAttributeOverrides AddIsNullableTrue(Type type)
{
    var xmlOverrides = new XmlAttributeOverrides();
    foreach (
        var prop in
            type.GetProperties()
                .Where(p => !p.PropertyType.IsValueType || p.PropertyType == typeof (string)))
    {
        var xmlAttributes = new XmlAttributes();
        var attr = prop.GetCustomAttribute<XmlElementAttribute>();
        if (attr != null)
        {
            attr.IsNullable = true;
        }
        else
        {
            attr = new XmlElementAttribute() {IsNullable = true};
        }
        xmlAttributes.XmlElements.Add(attr);
        xmlOverrides.Add(type, prop.Name, xmlAttributes);
    }
    return xmlOverrides;
}

Now when you create your XmlSerializer you just need to pass in the XmlAttributeOverrides

XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass), AddIsNullableTrue(typeof(TestClass)));

This example is assuming the class is called TestClass. Now you don't need to populate null fields and fields will be output to XML that are null.

Source Link
CharlesNRice
  • 4.4k
  • 15
  • 18

A different option is to fix this at the creation of the XmlSerializer. The XmlSerializer constructor can take a parameter for XmlAttributeOverrides

You could create a method to auto add the XmlElementAttribute, if missing, or set the IsNullable to true.

public static XmlAttributeOverrides AddIsNullableTrue(Type type)
{
    var xmlOverrides = new XmlAttributeOverrides();
    foreach (
        var prop in
            type.GetProperties()
                .Where(p => !p.PropertyType.IsValueType || p.PropertyType == typeof (string)))
    {
        var xmlAttributes = new XmlAttributes();
        var attr = prop.GetCustomAttribute<XmlElementAttribute>();
        if (attr != null && attr.IsNullable == false)
        {
            attr.IsNullable = true;
        }
        else
        {
            attr = new XmlElementAttribute() {IsNullable = true};
        }
        xmlAttributes.XmlElements.Add(attr);
        xmlOverrides.Add(type, prop.Name, xmlAttributes);
    }
    return xmlOverrides;
}

Now when you create your XmlSerializer you just need to pass in the XmlAttributeOverrides

XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass), AddIsNullableTrue(typeof(TestClass)));

This example is assuming the class is called TestClass. Now you don't need to populate null fields and fields will be output to XML that are null.