4

I have a piece of xml that looks something like

  <SubscriptionProduct>
    <SubscriptionProductIdentifier>
      <SubscriptionProductIdentifierType>
        <SubscriptionProductIDType>01</SubscriptionProductIDType>
        <ID>123456</ID>
        <Value>AAAA</Value>
      </SubscriptionProductIdentifierType>
      <SubscriptionProductIdentifierType xsi:nil="true" />
    </SubscriptionProductIdentifier>
    <SubscriptionProductDescription />
  </SubscriptionProduct>

As you can see the SubscriptionProductIdentifierType is a collection and in this case only contains one item.
How do I ignore the second empty item?

I've tried adding the xml ignore, however it removes the entire collection and I only want the second item in the collection removed if there is no data.

[System.Xml.Serialization.XmlIgnoreAttribute()]
public SubscriptionProductIdentifierType[] SubscriptionProductIdentifier {
    get {
        return this.subscriptionProductIdentifierField;
    }
    set {
        this.subscriptionProductIdentifierField = value;
    }
}

Any help would be very much appreciated.

Kind Regards Zal

svick
  • 236,525
  • 50
  • 385
  • 514
Zal
  • 43
  • 3
  • Good point from @ArsenMkrt. What's the length of the array? – Martijn B Jan 27 '12 at 12:15
  • I only assign two items to the array collection – Zal Jan 27 '12 at 12:25
  • If you are serializing (going from .NET to XML), you can try to add IsNullable = false on XmlRootAttribute to SubscriptionProductIdentifier. See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute.isnullable.aspx – Huusom Jan 27 '12 at 14:18

2 Answers2

1

There is not one item in your collection but two, one of which is null

just filter null items during addition, or even before return, depending on your business logic

public SubscriptionProductIdentifierType[] SubscriptionProductIdentifier {
    get {
        return this.subscriptionProductIdentifierField.Where(s=>s!=null).ToArray();
    }
...
}

Hope this helps

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • Thanks, unfortunately I don't have the .Where(is this from linq to sql) so in this case this is not helpful for this project :( – Zal Jan 27 '12 at 12:29
  • @Zal, you can filter without where clause, in for loop for example, the idea is to filter null values before serialization – Arsen Mkrtchyan Jan 27 '12 at 12:34
  • 1
    @Zal you should have the .where(...) it's plain LINQ not LINQ to SQL. Which version of the .NET are u using? – Martijn B Jan 27 '12 at 13:00
  • Sorry I wasn't thinking this morning. Adding a ref to the class to System.Linq showed the .Where. It clean and readable. Thank-you – Zal Jan 27 '12 at 15:49
0

XmlIgnoreAttribute will ignore the member, not just items that are null within an array. If you have no way of filtering the results or removing the null node ahead of time, then store a local variable to hold the filtered results and lazy load it.

private SubscriptionProductIdentifierType[] _subscriptionProductIdentifierField = null;
private SubscriptionProductIdentifierType[] _filteredSubscriptionProductIdentifier = null;

public SubscriptionProductIdentifierType[] SubscriptionProductIdentifier
{
    get { 
    return this._filteredSubscriptionProductIdentifier ?? (
        _filteredSubscriptionProductIdentifier = Array.FindAll(
            this._subscriptionProductIdentifierField, 
            delegate(SubscriptionProductIdentifierType t) { return t != null; } ));

}
    set
    {
        this._subscriptionProductIdentifierField = value;
        this._filteredSubscriptionProductIdentifier = null;
    }
} 
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83