static void SerializeIt(string data, string filename)
{
XmlSerializer serializer = new XmlSerializer(data.GetType());
using (XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8))
{
try
{
serializer.Serialize(writer, data);
Console.WriteLine("Serialization successful! Wrote test.dat!");
}
catch (InvalidOperationException)
{
Console.WriteLine("Failed to serialize object!!");
}
}
}
Sub SerializeIt(ByVal data As String, ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(String))
Using writer As New XmlTextWriter(filename, Encoding.UTF8)
Try
serializer.Serialize(writer, data)
Console.WriteLine("Serialization successful! Wrote test.dat!")
Catch ex As Exception
Console.WriteLine("Failed to serialize object!!")
End Try
End Using
End Sub
<?xml version="1.0" encoding="utf-8"?><string>This is a test</string>
As you can see in our result, the XmlSerializer and XmlTextWriter objects take care of formatting our output as valid XML. Our XML declaration is already included for us and the data of the object we serialized is clearly visible. Now as I said, this example is simple and it doesn't accurately portray the idea of gathering an object into one container. It does, however, give you an idea of the classes involved in XML serialization via .NET.
static string DeserializeIt(string filenme)
{
XmlSerializer serializer = new XmlSerializer(typeof(string));
string result = string.Empty;
using (XmlTextReader reader = new XmlTextReader(filenme))
{
try
{
result = serializer.Deserialize(reader).ToString();
Console.WriteLine("Deserialization successful! Got string: {0}", result);
}
catch (InvalidOperationException)
{
Console.WriteLine("Failed to deserialize object!!");
}
}
return result;
}
Function DeserializeIt(ByVal filename As String) As String
Dim serializer As New XmlSerializer(GetType(String))
Dim result As String = String.Empty
Using reader As New XmlTextReader(filename)
Try
result = DirectCast(serializer.Deserialize(reader), String)
Console.WriteLine("Deserialization successful! Got string: {0}", result)
Catch ex As Exception
Console.WriteLine("Failed to deserialize object!!")
End Try
End Using
Return result
End Function
This is a test
I apologize. I know that was a lot to read! You see that we really only changed the function called and captured its return value. You will notice that type of the object returned by Deserialize is of type object. Because of this, a cast will be required. For our examples, since we merely serialized/deserialized a string, we can simply call the ToString method on the object returned from Deserialize. Had we completed this process using some other object, then a cast would be required to store the result into the appropriate type of variable.
static void SerializeIt<T>(T data, string filename)
{
XmlSerializer serializer = new XmlSerializer(data.GetType());
using (XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8))
{
try
{
serializer.Serialize(writer, data);
Console.WriteLine("Serialization successful! Wrote test.dat!");
}
catch (InvalidOperationException)
{
Console.WriteLine("Failed to serialize object!!");
}
}
}
static T DeserializeIt<T>(string filenme)
where T: class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
T result = null;
using (XmlTextReader reader = new XmlTextReader(filenme))
{
try
{
result = (T)serializer.Deserialize(reader);
Console.WriteLine("Deserialization successful! Got string: {0}", result);
}
catch (InvalidOperationException)
{
Console.WriteLine("Failed to deserialize object!!");
}
}
return result;
}
Sub SerializeIt(Of T)(ByVal data As T, ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(T))
Using writer As New XmlTextWriter(filename, Encoding.UTF8)
Try
serializer.Serialize(writer, data)
Console.WriteLine("Serialization successful! Wrote test.dat!")
Catch ex As Exception
Console.WriteLine("Failed to serialize object!!")
End Try
End Using
End Sub
Function DeserializeIt(Of T)(ByVal filename As String) As T
Dim serializer As New XmlSerializer(GetType(T))
Dim result As T = Nothing
Using reader As New XmlTextReader(filename)
Try
result = DirectCast(serializer.Deserialize(reader), T)
Console.WriteLine("Deserialization successful! Got string: {0}", result)
Catch ex As Exception
Console.WriteLine("Failed to deserialize object!!")
End Try
End Using
Return result
End Function
As demonstrated above, we have opened up our serialization/deserializatHave a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)