How to deserialize xml to object in C#, If some xml missing in the xml
c#, serialization, xml
Solution
public class MyClass
{
[XmlElement(IsNullable = true)]
public string Group;
}
Problem
I had one xml payload, there are lot of xml elements available, I have declared all the elements in the class properties, when I tried to serialize it came with all nodes, Here I have removed some xml elements before deserialize, then am trying to deserialize the xml, here am getting error. Is that possible to deserialize without declared elements? I need to deserialize without some elements. Because deserialize xml payload is dynamic, it is coming from another vendor. Please help me. Code Block: Class: ``` [Serializable] public class CSSngSectorCable { [XmlElement("cableID")] public Int32 CSSngCableId { get; set; } public string RFDSSection { get; set; } public string RFDSSector { get; set; } public Int32 RFDSCableNumber { get; set; } [XmlElement("cableNumber")] public string CSSngCableNumber { get; set; } [XmlElement("feederType")] public string CSSngFeederType { get; set; } [XmlElement("feederLength")] public string CSSngFeederLength { get; set; } public int CableRowNumber { get; set; } } ``` Code to serialize: ``` var cables= new SectorCable(); ///added some code string serializevalue = SerializationHelper.SerializeToXml(cables, true); ``` Serialize Output: ``` <cable> <cableID>1</cableID> <cableNumber>2</cableNumber> <feederType>15.24</feederType> <feederLength></feederLength> </cable> ``` Data to deserialize: ``` <cable> <cableID>1</cableID> <cableNumber>2</cableNumber> </cable> ``` Code for deserialize: ``` public CSSngSectorCable GetCableFromDynamicXML(StreamReader reader) { try { var serializer = new XmlSerializer(typeof(CSSngSectorCable)); var cables = (CSSngSectorCable)serializer.Deserialize(reader); return cables; } catch (Exception ex) { throw; } ``` Here it's not working fine it's throw exception. How to resolve the issue.