简体   繁体   中英

Can't deserialize list of objects with XmlSerializer

i generated proxy classes of a wcf service with "visual studio > service reference" and i'm able to contact the service. One of the service operations return a compressed string in byte[] which represents a list of items.

PROBLEM: i can decompress the byte[], i can get an xml from the deserialized byte[] but i CAN'T deserialize objects, i get a list of objects with empty values.

here the xml string which i obtain from decompression

<?xml version="1.0"?>
<ArrayOfItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Item>
...
<FieldX> 
.. 
</FieldX> 
</Item>
</ArrayOfItem>

ArrayOfItem is not a class... but Item is:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://company.com/")]
public partial class Item: BaseObjectModel {

    private bool FieldX;

    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public bool FieldX{
        get {
            return this.FieldX;
        }
        set {
            this.FieldX= value;
            this.RaisePropertyChanged("FieldX");
        }
    }
}

My Code

        List<Item> lista = null;
        MemoryStream InStream = new MemoryStream(byteData);

        GZipStream gzDecompressed = new GZipStream(InStream, CompressionMode.Decompress, true);

        MemoryStream OutStream = new MemoryStream();

        //Retrieve the size of the decompressed file from the compressed footer

        byte[] bufferWrite = new byte[4];

        InStream.Position = (int)InStream.Length - 4;

        InStream.Read(bufferWrite, 0, 4);

        InStream.Position = 0;

        //Convert to int for using in declaring our Byte[] size

        int bufferLength = BitConverter.ToInt32(bufferWrite, 0);

        //1MB Buffer

        byte[] buffer = new byte[1024 * 1024];

        while (true)
        {

            int bytesRead = gzDecompressed.Read(buffer, 0, buffer.Length);



            // If we reached the end of the data

            if (bytesRead == 0) break;

            OutStream.Write(buffer, 0, bytesRead);

        }

        // Close the streams

        InStream.Close();

        gzDecompressed.Close();

        OutStream.Position = 0;

        var sr = new StreamReader(OutStream);
        string myStr = sr.ReadToEnd();

        OutStream.Position = 0;



        XmlSerializer serializer = new XmlSerializer(typeof(List<Item>));
        XmlReader read = XmlReader.Create(OutStream);
        List<Item> lista2 = (List<Item>)serializer.Deserialize(read);

The string it's only to see what the service gives, i use memory stream to deserialize.

I get a list with the right number of item but every item has empty properties....

Any help is appreciated, thanks.

EDIT

I tried to serialize the list using the Item object of the proxy and i see that the xml has different encoding

The xml from the service

<?xml version="1.0"?><ArrayOfItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Item>...

The xml serialized by me

<?xml version="1.0" encoding="utf-16"?><ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Item xmls="company.com">

The easiest way to diagnose this issue is to simply serialize a dummy list of your objects, and look at the produced XML, then compare it with what you are attempting to deserialize.

Once you serialize it, you'll know what the XML serialization library expects the XML to look like based on your annotations.

BTW, you don't need to type "Attribute" in "XmlElementAttribute"... C# "knows" about this common naming pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM