Hi,
I use the following Extension to Serialize object to string.
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.