private string DeSerialize(string studata)
{
dcs= new DataContractSerializer(typeof(string));
try
{
using (var q = new StringReader(studata))
{
using (XmlReader reader = XmlReader.Create(sr))
{
var k = dcs.ReadObject(reader);
return k.ToString();
}
}
}
catch
{}
return string.Empty;
}
with the above method got the runtime error “Error in line 1 position 41. Expecting element ‘string’ from namespace ‘http://schemas.microsoft.com/2003/10/Serialization/’.. Encountered ‘Element’ student namespace
When you serialize something, you serialize an object into some representation such as XML, JSON, etc. If the representation is not binary, the result of serialization can be stored in a string.
Your code is trying to take a string and deserialize it into a string. That does not make sense.
The line
states that the type of the serialized object you are deserializing is type string. That’s probably not what you meant.
Change
typeof(string)totypeof(WhateverTypeIPreviouslySerialized).Also, the return type of
DeSerializeisstring. That should also beWhateverTypeIPreviouslySerialized.Unrelated to the core question, I would point out that the empty catch
is bad practice. It will mask Exceptions that are being thrown and need to be dealt with. If you wanted to return a specific value when serialization fails, put that value inside the catch statement.