質問

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

役に立ちましたか?

解決

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

他のヒント

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;
    }
} 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top