none
Why can't I serialize strings, points, and datatimes? RRS feed

  • Question

  • I was moving some classes to a class library and it's probably refreshes the compiler because now it generates all sort of warning  about my serializable class properties, the following properties gives a warning:

     public Point Location { get; set; } = new Point(0, 0);
            public DateTime HiredTime { get; set; } = DateTime.Now;
            public List<string> RoleTags { get; set; } = new List<string>();
            public List<string> ProffesionTags { get; set; } = new List<string>();
            public string Description { get; set; } = String.Empty;
    public string FirstName { get; set; } = String.Empty;
            public string LastName { get; set; } = String.Empty;

    the warning are:

    Field Location is a member of type (The main class ) which is serializable but is of type System.Drawing.Point which is not serializable

    and same for all the rest of the properties.

    I have noticed that type point is not serializing, but string and datetime seem to sterilize well so far, so why there are warnings? Did I broke something by moving the classes to a class library?


    • Edited by avivgood Monday, October 21, 2019 2:35 PM
    Monday, October 21, 2019 2:30 PM

All replies

  • Try this 

    public class Birthdays
    {
      [XmlElement(DataType="date")]
      public DateTime DateOfBirth {get;set;}
      public string Name {get;set;}
    }

    Results

    <?xml version="1.0" encoding="utf-16"?>
    <Birthdays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <DateOfBirth>2013-11-14</DateOfBirth>
      <Name>John Smith</Name>
    </Birthdays> 

    Source

    https://stackoverflow.com/questions/19983302/how-to-serialize-xml-date-only-from-datetime-in-c-sharp

    See also

    https://stackoverflow.com/questions/21865610/how-to-serialize-the-point


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 21, 2019 3:35 PM
    Moderator
  • Hi avivgood,

    Thank you for posting here.

    Besed on your code, I try to serialize this object into JSON and XML files, but it works well without any warnings or errors.

    So could you provide more information so that I can reproduce your problem?

    Looking forward to your reply.

    Best regards,

    Timon

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 8:10 AM