I'm trying to deserialize and XML file that is validated to an XSD. But the thing is that one of the nodes can be empty from time to time so I came up with this XSD now
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InterfaceLog">
<xs:complexType>
<xs:sequence>
<xs:element name="Log" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="InterfaceID" type="xs:string"/>
This line --> <xs:element name="PrOrNumber" type="int-or-empty"/>
<xs:element name="MESKey" type="xs:string"/>
<xs:element name="MsgStatusCode" type="xs:string"/>
<xs:element name="MsgClass" type="xs:string"/>
<xs:element name="MsgNumber" type="xs:string"/>
<xs:element name="MsgDescription" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="int-or-empty">
<xs:union memberTypes="xs:int empty-string" />
</xs:simpleType>
<xs:simpleType name="empty-string">
<xs:restriction base="xs:string">
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
The line PrOrNumber can be empty from time to time so I came up with the simpleType int-or-empty. So now when I try to deserialize the XML , it fails when attempting to deserialize the PrOrNumber (returns error input string is not of valid format).
XML that I'm trying to deserialize
<?xml version="1.0" encoding="utf-8" ?>
<ns2:InterfaceLog xmlns:ns2="foo.com/foo">
<Log>
<InterfaceID>InterFace_Id</InterfaceID>
<PrOrNumber/>
<MESKey>Run</MESKey>
<MsgStatusCode>S</MsgStatusCode>
<MsgClass>Class</MsgClass>
<MsgNumber>123</MsgNumber>
<MsgDescription>Description</MsgDescription>
</Log>
</ns2:InterfaceLog>
And the class that I'm trying to deserialize to
[XmlRoot("InterfaceLog", Namespace = "foo.com/foo")]
[Serializable]
public class InterfaceLog
{
[XmlElement("Log", Namespace = "")]
public List<Log> LogCollection { get; set; }
}
[XmlRoot("Log")]
[Serializable]
public class Log
{
public Log() { }
[XmlElement("InterfaceID")]
public string InterfaceID { get; set; }
[XmlElement("PrOrNumber")]
public string PrOrNumber { get; set; }
[XmlElement("MESKey")]
public string MESKey { get; set; }
[XmlElement("MsgStatusCode")]
public string MsgStatusCode { get; set; }
[XmlElement("MsgClass")]
public string MsgClass { get; set; }
[XmlElement("MsgNumber")]
public string MsgNumber { get; set; }
[XmlElement("MsgDescription")]
public string MsgDescription { get; set; }
}
And the function that does the deserialization
InterfaceLog interfaceLogCollection = xmlString.Deserialize<InterfaceLog>();
public static T Deserialize<T>(this string serializedObj) where T : class
{
XmlSerializer xs = new XmlSerializer(typeof(T));
T result;
using (TextReader reader = new StringReader(serializedObj))
result = xs.Deserialize(reader) as T;
return result;
}
What I've tried
- Changing the type to integer , decimal etc..
- Changing the type to string
- Commenting the line works but that is not a solution
- Setting the XSD tag on Nillable
The thing is I can't change the input XML so adding the xsi:nil="true"
tag is not an option