Ignorieren des Nullwerts in der XML-Serialisierung
-
14-11-2019 - |
Frage
Ich habe ein Stück XML, das so etwas aussieht generasacodicetagpre.
Wie Sie sehen, ist das SubscriptionProductIdentifiertyp eine Sammlung und enthält in diesem Fall nur ein Artikel.
Wie ignoriere ich den zweiten leeren Artikel?
Ich habe versucht, den XML Ignorieren hinzuzufügen, aber es entfernt jedoch die gesamte Sammlung und ich möchte nur, dass der zweite Element in der Sammlung entfernt, wenn keine Daten vorhanden sind. generasacodicetagpre.
Jede Hilfe wäre sehr geschätzt.
freundliche Grüße Zal
Lösung
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
Andere Tipps
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;
}
}