简体   繁体   English

无法使用XmlSerializer反序列化对象列表

[英]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. 我使用“ Visual Studio>服务参考”生成了wcf服务的代理类,并且可以联系该服务。 One of the service operations return a compressed string in byte[] which represents a list of items. 服务操作之一返回以byte []表示项目列表的压缩字符串。

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. 问题:我可以解压缩byte [],我可以从反序列化后的byte []获取xml,但是我不能反序列化对象,而是得到具有空值的对象列表。

here the xml string which i obtain from decompression 这是我从解压缩获得的xml字符串

<?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: ArrayOfItem不是类...但是Item是:

[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 我尝试使用代理的Item对象序列化列表,并且看到xml具有不同的编码

The xml from the service 服务中的xml

<?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

<?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. 诊断此问题的最简单方法是简单地序列化对象的虚拟列表,然后查看生成的XML,然后将其与尝试反序列化的内容进行比较。

Once you serialize it, you'll know what the XML serialization library expects the XML to look like based on your annotations. 序列化之后,您将知道XML序列化库根据您的注释期望XML的外观。

BTW, you don't need to type "Attribute" in "XmlElementAttribute"... C# "knows" about this common naming pattern. 顺便说一句,您不需要在“ XmlElementAttribute”中键入“ Attribute” ... C#“了解”此通用命名模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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