I want to serialize my class having property IPAddress.
public class Person
{
public IPAddress MasterIP { get; set; }
public Person(){}
}
public void SerializeMyClass()
{
Person obj=new Person();
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(obj);
}
But I am getting error:
"can not serialize IPaddress because it has no empty constructor"
You can't - XmlSerializer can only serialize public properties/fields of public classes that have public parameterless constructors. You can either look into other serializer type (like BinaryFormatter) or hack your class like:
public class Person {
[XmlIgnore]
public IPAddress MasterIP { get; set; }
[XmlElement("MasterIP")]
public string MasterIPForXml {
get { return MasterIP.ToString(); }
set { MasterIP = string.IsNullOrEmpty(value) ? null :
IPAddress.Parse(value);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With