Cómo controlar el comportamiento de serialización XML en xsi: nil =
-
20-08-2019 - |
Pregunta
Estoy trabajando en un servicio web para compartir datos entre 2 sistemas ERP. El primer ERP llama al servicio web, que serializa el objeto de datos y lo envía al segundo ERP.
Un objeto de datos tiene este aspecto:
<xs:complexType name="Parent">
<xs:sequence>
<xs:element ref="ta:ReceiptLine" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Child">
<xs:sequence>
...
<xs:element name="SerialNo" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="Quantity" type="xs:int" nillable="false"/>
...
</xs:sequence>
</xs:complexType>
...
<xs:element name="Child" type="ta:Child" nillable="true"/>
Las clases generadas por XSD:
[System.Serializable]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=false)]
public partial class Parent {
private Child[] child;
[System.Xml.Serialization.XmlElementAttribute("Child", IsNullable=true)]
public Child[] Child {
get {return this.child;}
set {this.child = value;}
}
[System.Serializable]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=true)]
public partial class Child{
private string serialNo;
private int quantity;
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string SerialNo {
get {return this.serialNo;}
set {this.serialNo = value;}
}
public int Quantity {
get { return this.quantity;}
set {this.quantity = value;}
}
}
Estoy serializando mis objetos de datos con XmlSerializer
El problema es : (en serialización) Cada vez que el objeto Child está vacío (xsi: nil = " true ") XSD genera la estructura Child completa de todos modos. Y debido a que la Cantidad no es nulable / anulable, XSD escribe 0 como valor ... De esta manera:
<Parent>
<Child xsi:nil="true">
<SerialNo xsi:nil="true" />
<Quantity>0</Quantity>
</Child>
</Parent>
Esperaba obtener algo como esto:
<Parent>
</Child xsi:nil="true">
</Parent>
La pregunta es : ¿Hay alguna manera de evitar que XSD analice un xsi: nil = " true " -Object ??
¿Alguna sugerencia?
TIA
Solución
ok
¡Lo tengo ahora! ¡Debe marcar la propiedad Cantidad con el XmlElementAttribute explícitamente!
[XmlElement(IsNullable=false)]
public int Quantity {
get { return this.quantity;}
set {this.quantity = value;}
}
No tengo idea de por qué esto no se ha generado automáticamente ...