簡體   English   中英

無法使用XmlSerializer反序列化對象列表

[英]Can't deserialize list of objects with XmlSerializer

我使用“ Visual Studio>服務參考”生成了wcf服務的代理類,並且可以聯系該服務。 服務操作之一返回以byte []表示項目列表的壓縮字符串。

問題:我可以解壓縮byte [],我可以從反序列化后的byte []獲取xml,但是我不能反序列化對象,而是得到具有空值的對象列表。

這是我從解壓縮獲得的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不是類...但是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");
        }
    }
}

我的密碼

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

該字符串只是查看服務提供的內容,我使用內存流進行反序列化。

我得到一個帶有正確數量的項目的列表,但是每個項目都有空的屬性。

任何幫助表示贊賞,謝謝。

編輯

我嘗試使用代理的Item對象序列化列表,並且看到xml具有不同的編碼

服務中的xml

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

我序列化的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">

診斷此問題的最簡單方法是簡單地序列化對象的虛擬列表,然后查看生成的XML,然后將其與嘗試反序列化的內容進行比較。

序列化之后,您將知道XML序列化庫根據您的注釋期望XML的外觀。

順便說一句,您不需要在“ XmlElementAttribute”中鍵入“ Attribute” ... C#“了解”此通用命名模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM