none
Problem in XML Serialization of a C# class. RRS feed

  • Question

  • I am trying to Xml Serialize a class "Device".

    i used getting exception in the line 

    System.Xml.Serialization.XmlSerializer xmlserializer = new System.Xml.Serialization.XmlSerializer(typeof(Device));

    Following exceptions i am getting : - 

    Managed Debugging Assistant 'BindingFailure' has detected a problem in 'P:\IAMS_NEW\Tools\FDMTemplateGenerationTool\TemplateGeneration\bin\Debug\TemplateGeneration.exe'.
    Additional Information: The assembly with display name 'InformationModel.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'InformationModel.XmlSerializers, Version=440.1.14.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    Then on Pressing Continue

    A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

    Additional information: Could not load file or assembly 'InformationModel.XmlSerializers, Version=440.1.14.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    Then on Pressing Continue

    Managed Debugging Assistant 'BindingFailure' has detected a problem in 'P:\IAMS_NEW\Tools\FDMTemplateGenerationTool\TemplateGeneration\bin\Debug\TemplateGeneration.exe'.
    Additional Information: The assembly with display name 'InformationModel.XmlSerializers' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'InformationModel.XmlSerializers' or one of its dependencies. The system cannot find the file specified.

    Then on Pressing Continue

    A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

    Additional information: Could not load file or assembly 'InformationModel.XmlSerializers' or one of its dependencies. The system cannot find the file specified.

    Note :-

    In this Device class that i am trying to xml Serialize uses som other class objects that contain the Dictionary, for that i have written a code to serialize as follows :-

    [Serializable]
        [XmlRoot("dictionary")]
        public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
        {
            private const string DEFAULTITEMTAG = "item";
            private const string DEFAULTKEYTAG = "key";
            private const string DEFAULTVALUETAG = "value";

            private static readonly XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            private static readonly XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            public SerializableDictionary()
                : base()
            {
            }

            public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
                : base(dictionary)
            {
            }

            public SerializableDictionary(IEqualityComparer<TKey> comparer)
                : base(comparer)
            {
            }

            public SerializableDictionary(int capacity)
                : base(capacity)
            {
            }

            public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
                : base(dictionary, comparer)
            {
            }

            public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
                : base(capacity, comparer)
            {
            }

            protected SerializableDictionary(SerializationInfo info, StreamingContext context)
                : base(info, context)
            {
            }

            protected virtual string ItemTagName
            {
                get
                {
                    return DEFAULTITEMTAG;
                }
            }

            protected virtual string KeyTagName
            {
                get
                {
                    return DEFAULTKEYTAG;
                }
            }

            protected virtual string ValueTagName
            {
                get
                {
                    return DEFAULTVALUETAG;
                }
            }

            public XmlSchema GetSchema()
            {
                return null;
            }

            public void ReadXml(XmlReader reader)
            {
                var wasEmpty = reader.IsEmptyElement;

                reader.Read();

                if (wasEmpty)
                {
                    return;
                }

                try
                {
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.ReadStartElement(this.ItemTagName);
                        try
                        {
                            TKey key;
                            TValue value;

                            reader.ReadStartElement(this.KeyTagName);
                            try
                            {
                                key = (TKey)keySerializer.Deserialize(reader);
                            }
                            finally
                            {
                                reader.ReadEndElement();
                            }

                            reader.ReadStartElement(this.ValueTagName);
                            try
                            {
                                value = (TValue)valueSerializer.Deserialize(reader);
                            }
                            finally
                            {
                                reader.ReadEndElement();
                            }

                            this.Add(key, value);
                        }
                        finally
                        {
                            reader.ReadEndElement();
                        }

                        reader.MoveToContent();
                    }
                }
                finally
                {
                    reader.ReadEndElement();
                }
            }

            public void WriteXml(XmlWriter writer)
            {
                foreach (var keyValuePair in this)
                {
                    writer.WriteStartElement(this.ItemTagName);
                    try
                    {
                        writer.WriteStartElement(this.KeyTagName);
                        try
                        {
                            keySerializer.Serialize(writer, keyValuePair.Key);
                        }
                        finally
                        {
                            writer.WriteEndElement();
                        }

                        writer.WriteStartElement(this.ValueTagName);
                        try
                        {
                            valueSerializer.Serialize(writer, keyValuePair.Value);
                        }
                        finally
                        {
                            writer.WriteEndElement();
                        }
                    }
                    finally
                    {
                        writer.WriteEndElement();
                    }
                }
            }
        }

    Please Let me know now what can be the reason for still i am getting the Exceptions as listed above.

    Tuesday, March 19, 2013 6:33 AM

Answers

  • Hi Anant,

    Welcome to the MSDN Forum.

    The exception message indicates that there is no assembly "InformationModel.XmlSerializers, Version=440.1.14.0, Culture=neutral, PublicKeyToken=null"  

    So would you like to check your assembly references, where do you refer this assembly?

    Please check where do you use the "InformationModel.XmlSerializers" namespace?

    Please make sure all your "XmlSerializers" object refers to the right type.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 20, 2013 8:59 AM
    Moderator