0

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)
Sss
  • 1,519
  • 8
  • 37
  • 67
  • Does this link help at all? http://stackoverflow.com/questions/4726208/deserialization-error-in-xml-document1-1 – DavidG May 22 '14 at 08:37
  • Does the error have an `InnerException`? – Sayse May 22 '14 at 08:37
  • @DavidG - likely not, 99% it is BOM issue (first character of the string is BOM mark, not `<` as required). It is just wrong to convert stream to string the way shown in the post (likely OP knows it too, so comment). – Alexei Levenkov May 22 '14 at 08:39
  • User, I can't read tiny french, please update your question with the text from this error.. (Preferably translated) – Sayse May 22 '14 at 08:39
  • @Sayse Ok met me update. – Sss May 22 '14 at 08:41
  • @DavidG when itried to replace stringreader to StreamReader i got exception: {System.ArgumentException: Illegal characters in path corresponding that line. – Sss May 22 '14 at 08:43
  • I've tried to reconstruct the error you are getting but I've got: `System.NullReferenceException` on line: `Parameter = { new Parameter { Name = "Name1", Label = "Label1", Unit = "Uint1", ... }` – rosko May 22 '14 at 08:49
  • @Sayse please see th edit for exception details – Sss May 22 '14 at 08:49
  • @rosko I have added all the classes i am using ..Please see th eedited code. – Sss May 22 '14 at 08:53
  • I don't have time right now to fully look into it but I can guarrantee it isn't to do with the first character in the xml, its either the root element it is expecting is different to the one given or something similar to that. Its also possible that the xmlreader is set up incorrectly.. Again there might be an innerexception in the inner exception – Sayse May 22 '14 at 08:55
  • @user234839 In your encoding, if you replace the `UTF8` class with `Unicode`, does that help? – DavidG May 22 '14 at 08:56
  • @DavidG It is successfully serialized i can see the xml obtained. Still is it possible that i may have problem in deserializin due to this. – Sss May 22 '14 at 08:58
  • @Sayse I have updated all classes. I guess it is done properly because it would have created problem while serializinf if it was so. – Sss May 22 '14 at 08:59
  • @user234839 Exactly, I think the StringReader uses unicode which is causing your problem. Try encoding in unicode and let us know! – DavidG May 22 '14 at 09:00
  • I've copy-pasted your code into VS and it's 100% working for me... it's serializing your xmlstring into xml and then deserialize. I can get any value from `parameter` and print it to the console so I think that now I don't know what is your problem. – rosko May 22 '14 at 09:01
  • can you see all the attributes and names by the object obtained on debugging ? – Sss May 22 '14 at 09:02
  • @DavidG could you please suggest me good link to implement that or could you please write piece of code for reference ? Thanks – Sss May 22 '14 at 09:07

1 Answers1

1

Afterall i found soluton for it. Reaplacing this:

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);

with

var writer = new StringWriter();
            var serializer = new XmlSerializer((objectToSerialize.GetType()));
            serializer.Serialize(writer, objectToSerialize);
            string xml = writer.ToString();
            return xml;

worked because StringReader is in compatible with utf8.

Sss
  • 1,519
  • 8
  • 37
  • 67