I am using Silverlight for my serializing and deserializing.
I ahve succesfully done serializing by obtaining objects from the class and the xml which i obtained on serializing , the same xml i am trying to deserialize.
My code to do so is:
namespace SliderLastTry
{
public class ControlClass
{
public void Main()
{
Parameters pc = new Parameters()
{
Parameter = { new Parameter { Name = "Name1", Label = "Label1", Unit = "Uint1", Component = { new Component { Type = "Type1", Attributes = { new Attributes { Type = "Combo", Displayed = "42", Selected = "02", Items = { "10", "11", "12", "13", "14" } } } } } }, { new Parameter { Name = "Name2", Label = "Label2", Unit = "Uint2", Component = { new Component { Type = "Type2", Attributes = { new Attributes { Type = "Slider", Displayed = "52", Selected = "05", Items = { "20", "21", "22", "23", "24" } } } } } } } }
,
Separator = { new Separator { Separators = "AutoSkew1" } }
};
String xml = pc.ToXml(); //This function serializes and returns xml.
XmlSerializer deserializer = new XmlSerializer(typeof(Parameters));
XmlReader reader = XmlReader.Create(new StringReader(xml));
Parameters parameter = (Parameters)deserializer.Deserialize(reader);
}
}
This line in this code gives me exception:
Parameters parameter = (Parameters)deserializer.Deserialize(reader);
Thsi is code for serializing (I mean the class where MyXML is defined):
namespace SliderLastTry
{
public static class Xml
{
public static string ToXml(this object objectToSerialize)
{
var memory = new MemoryStream();
var serial = new XmlSerializer(objectToSerialize.GetType());
serial.Serialize(memory, objectToSerialize);
var utf8 = new UTF8Encoding();
return utf8.GetString(memory.GetBuffer(), 0, (int)memory.Length);
}
}
}
And the warning obtained is:
There is an error in XML document (1, 1).
Please see the exception in details:
Inner Exception:
{System.Xml.XmlException: Invalid data at root level. Line 1, position 1.
to System.Xml.XmlTextReaderImpl.Throw (Exception e)
to System.Xml.XmlTextReaderImpl.Throw (String res, String arg)
System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace to ()
System.Xml.XmlTextReaderImpl.ParseDocumentContent to ()
System.Xml.XmlTextReaderImpl.Read to ()
System.Xml.XmlReader.MoveToContent to ()
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderParameters.Read7_parameters to ()}
At stack Trace:
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle, Object events)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader)
SliderLastTry.ControlClass.Main to ()
ctor to SliderLastTry.MainPage .. ()
at SliderLastTry.App.Application_Startup (Object sender, e StartupEventArgs)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler (UInt32 indexType, handlerDelegate Delegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent (unmanagedObj IntPtr, IntPtr unmanagedObjArgs, argsTypeIndex Int32, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)