none
DataContractSerializer failed in namespace xmlns= .... RRS feed

  • Question

  • hello,

    I've this file XML:

    <?xml version="1.0" encoding="utf-8"?>
    <SettingsDataSource xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MioProgramma">
      <Params>
        <DATOverwrite>false</DATOverwrite>
        <DataSize>0</DataSize>
        <Name>vNomeFile_gauc</Name>
        <Size>0</Size>
      </Params>
      <Params2>
        <Test>Ciao</Test>
      </Params2>
    </SettingsDataSource>

    Data are rappresented by this class:

    public class SettingsDataSource
        {
            public DownloadParams Params { get; set; }
    
            public DownloadParams2 Params2 { get; set; }
        }
    
        [DataContract]
        public class DownloadParams
        {
            [DataMember]
            public bool DATOverwrite { get; set; }
    
            [DataMember]
            public int Size { get; set; }
    
            [DataMember]
            public int DataSize { get; set; }
    
            [DataMember]
            public string Name { get; set; }
        }
    
        [DataContract]
        public class DownloadParams2
        {
            [DataMember]
            public string Test { get; set; }
        }

    To serialize and deserialize I'm using the following method:

    public class SettingsService
        {
            public SettingsDataSource Load(string filename)
            {
                SettingsDataSource settings = new SettingsDataSource();
                if (File.Exists(filename))
                {
                    string encrypted = File.ReadAllText(filename);
                    string decrypted = encrypted;
    
                    using (Stream stream = new MemoryStream())
                    {
                        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(decrypted);
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Position = 0;
                        DataContractSerializer serializer = new DataContractSerializer(typeof(SettingsDataSource));
                        var data = (SettingsDataSource)serializer.ReadObject(stream);
                        settings = data;
                    }
                }
    
                return settings;
            }
    
            public void Save(SettingsDataSource settings, string filename)
            {
                string text;
                DataContractSerializer serializer = new DataContractSerializer(typeof(SettingsDataSource));
                using (var ms = new MemoryStream())
                {
                    using (var xmlWriter = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true }))
                    {
                        serializer.WriteObject(xmlWriter, settings);
                    }
                    ms.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(ms))
                    {
                        text = reader.ReadToEnd();
                    }
    
                    File.WriteAllText(filename, text);
                }
            }
        }

    My problem is that if my program named ("MioProgramma") serialize and deserialize file, run perfectly.

    If another program (for example "MioProgramma2"), deserialize the XML file, failed while read :

    xmlns="http://schemas.datacontract.org/2004/07/MioProgramma"

    Is it possible to obtain deserilization is indipendent to this namespace (in such a way that any program can deserialize my file?) ?

    thanks

    11

    22

    Friday, January 15, 2016 8:12 AM

Answers

  • Try the next definitions in both of your programs:

    [DataContract( Name = nameof( SettingsDataSource ), Namespace = "MioPrograma" )]

    public class SettingsDataSource

    {

           [DataMember]

           public DownloadParams Params { get; set; }

           [DataMember]

           public DownloadParams2 Params2 { get; set; }

    }

    [DataContract( Namespace = "MioPrograma" )]

    public class DownloadParams

    {

           [DataMember]

           public bool DATOverwrite { get; set; }

           [DataMember]

           public int Size { get; set; }

           [DataMember]

           public int DataSize { get; set; }

           [DataMember]

           public string Name { get; set; }

    }

    [DataContract( Namespace = "MioPrograma" )]

    public class DownloadParams2

    {

           [DataMember]

           public string Test { get; set; }

    }

    And perhaps consider a simplified serialization like this:

    // writting:

    var xws = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

    using( var writer = XmlWriter.Create( filename, xws ) )

    {

           DataContractSerializer serializer = new DataContractSerializer( typeof( SettingsDataSource ) );

           serializer.WriteObject( writer, settings );

    }


    // reading:

    using( var reader = XmlReader.Create( filename ) )

    {

           DataContractSerializer serializer = new DataContractSerializer( typeof( SettingsDataSource ) );

           settings = (SettingsDataSource)serializer.ReadObject( reader );

    }



    • Edited by Viorel_MVP Friday, January 15, 2016 9:13 AM
    • Marked as answer by stnf1974 Friday, January 15, 2016 12:58 PM
    Friday, January 15, 2016 9:12 AM