Вопрос

У меня есть кусок XML, который выглядит что-то вроде

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

Как вы можете увидеть, что подпискиПродуктеквентификация - это коллекция, и в этом случае содержится только один элемент.
Как я могу игнорировать второй пустой элемент?

Я пытался добавить XML Igramore, однако он удаляет всю коллекцию, и я хочу только второй элемент в коллекции удален, если нет данных.

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

Любая помощь была бы очень ценится.

Добры 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