Error when deserializing xml to an object: System.FormatException Input String was not in correct format Error when deserializing xml to an object: System.FormatException Input String was not in correct format xml xml

Error when deserializing xml to an object: System.FormatException Input String was not in correct format


The problem is that the property GroupID is declared as an integer, but its value is empty (<GroupID/>). One solution is to change the XML so that the value for that element is a valid integer:

<GroupID>0</GroupID>

Another solution would be to deserialize that value as a string, and convert to an integer which users of the type may consume:

    [XmlRoot("Server")]    public class RegisterServerObject    {        public RegisterServerObject() { }        public int ServerID { get; set; }        [XmlIgnore]        public int GroupID { get; set; }        [XmlElement(ElementName = "GroupID")]        public string GroupIDStr        {            get            {                return this.GroupID.ToString();            }            set            {                if (string.IsNullOrEmpty(value))                {                    this.GroupID = 0;                }                else                {                    this.GroupID = int.Parse(value);                }            }        }        public int ParentID { get; set; }        public string ServerName { get; set; }        public string User { get; set; }        public int Uid { get; set; }        public string Domain { get; set; }        public string Location { get; set; }        [XmlArray(ElementName = "AssociatedModules")]        [XmlArrayItem(ElementName = "Module")]        public List<RegisterModuleObject> AssociatedModules { get; set; }    }    public class RegisterModuleObject    {        public int ModId { get; set; }        public int ServerId { get; set; }        public string ModName { get; set; }        public int ModuleStatus { get; set; }    }


First you need to set XmlIgnore attribute for GroupID property

[XmlRoot("Server")]public class RegisterServerObject{   . . .   [XmlIgnore]   public int GroupID { get; set; }   . . .}

then you can create adapter that you will use for deserialization:

[XmlRoot("Server")]public class RegisterServerObjectAdapter : RegisterServerObject{    [XmlElement("GroupID")]    public string GroupIDNew    {        get { return GroupID.ToString(); }        set        {            int outInt;            int.TryParse(value, out outInt);            GroupID = outInt;        }    }}

and finaly I have modified DeserializeSingleServerFromXml method a bit

    static RegisterServerObject DeserializeSingleServerFromXml(XDocument serverElement)    {        var deserializer = new XmlSerializer(typeof(RegisterServerObjectAdapter));        return (RegisterServerObject)deserializer.Deserialize(serverElement.CreateReader(ReaderOptions.OmitDuplicateNamespaces));    }


Could it be the GroupID that it's having issues with. Since no value for GroupID is provided, I believe it tries to pass it as a string.Empty which obviously wouldn't set to an int property.

You could try the following:

[XmlIgnore]public int GroupID {    get { !string.IsNullOrEmpty(GroupIDAsText) ? int.Parse(GroupIDAsText) : 0; }    set { GroupID = value; }}[XmlAttribute("GroupID")public int GroupIDAsText { get; set; }

.