Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using custom linked list, where I can able to serialize instance of Pen, Point etc.
But if I take Point and Pen object in simple class (without linked list), I can not serialize instance of Point and Pen.

Linked List class is like this


MIDL
[Serializable]
public class LinkedList
{
    // various methods to maintain First, Last, Next, Previous Node information like
    public void Add(Node node)
      {
        ......
    }
    public void Remove(Node node)
      {
        ......
    }

    //Node classs
    [Serializable]
      public class Node
      {
        public Point PointData
            {
                get;
                set;
            }
        public Node Next
            {
                get;
            set;
            }
    }
}


I can serialize LinkedList instance using below code
BinaryFormatter formatter = new BinaryFormatter()
formatter.Serialize(file, objLinkedList);


It is working fine, but if I use class like

C#
[Serializable]
 public class OverlayPoint
 {
    public Point ObjPoint
       {
            get;
            set;
       }
       public Pen ObjPen
       {
            get;
            set;
       }
}


Then I cannot serialize showing error like

Type 'System.Drawing.Pen' in Assembly 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

Just because of internal class in linked list it is not firing error.
I could not able understand why this happened

Thanks for your time
Posted

1 solution

Hi,

I use the following Extension to Serialize object to string.

C#
public static string SerializeObjectToString<T>(this T objectToSerialize)
        {
            StringWriter outStream = new StringWriter();
            string value;
            try
            {
                XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
                s.Serialize(outStream, objectToSerialize);
                value = outStream.ToString();
            }
            finally
            {
                outStream.Close();
            }
            return value;
        }


To then serialize your object you would use like follow.

Node myNode = new Node {PointData = new Point(10, 50), Next = new Node {PointData = new Point(30, 50)}};

Console.WriteLine(myNode.SerializeObjectToString());


View result below.

<br />
"<?xml version=\"1.0\" encoding=\"utf-16\"?><br />
<Node xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">  <PointData><br />
    <X>10</X><br />
    <Y>50</Y><br />
    </PointData><br />
    <Next><br />
        <PointData><br />
            <X>30</X><br />
            <Y>50</Y><br />
        </PointData><br />
    </Next><br />
</Node><br />


Hope this helps.
 
Share this answer
 
Comments
Plinio Guimarães 2-Sep-13 10:08am    
How can I serialize System.Drawing.Point to result X,Y as attributes in XML? Example:
<node x="30" y="50/">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900