XML deserialization in C# is returning null array even when XML has data

Asked

Viewed 134 times

1

I’m trying to deserialize XML

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:catalogProductListResponse>
         <storeView SOAP-ENC:arrayType="ns1:catalogProductEntity[609]" xsi:type="ns1:catalogProductEntityArray">
            <item xsi:type="ns1:catalogProductEntity">
               <product_id xsi:type="xsd:string">36</product_id>
               <sku xsi:type="xsd:string">000975</sku>
               <name xsi:type="xsd:string">Beauty Dermo HTM - Vacuoterapia</name>
               <set xsi:type="xsd:string">4</set>
               <type xsi:type="xsd:string">simple</type>
               <category_ids SOAP-ENC:arrayType="xsd:string[9]" xsi:type="ns1:ArrayOfString">
                  <item xsi:type="xsd:string">4</item>
               </category_ids>
               <website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
                  <item xsi:type="xsd:string">1</item>
               </website_ids>
            </item>
            <item xsi:type="ns1:catalogProductEntity">
               <product_id xsi:type="xsd:string">37</product_id>
               <sku xsi:type="xsd:string">004279</sku>
               <name xsi:type="xsd:string">Effect HTM - Radiofrequência</name>
               <set xsi:type="xsd:string">4</set>
               <type xsi:type="xsd:string">simple</type>
               <category_ids SOAP-ENC:arrayType="xsd:string[8]" xsi:type="ns1:ArrayOfString">
                  <item xsi:type="xsd:string">4</item>
               </category_ids>
               <website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
                  <item xsi:type="xsd:string">1</item>
               </website_ids>
            </item>
            ...[OTHER MANY ITENS]
         </storeView>
      </ns1:catalogProductListResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

In class

public class ProductListResponse
{
    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
    public partial class Envelope
    {
        private EnvelopeBody bodyField;

        private string encodingStyleField;

        /// <remarks/>
        public EnvelopeBody Body
        {
            get
            {
                return this.bodyField;
            }
            set
            {
                this.bodyField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
        public string encodingStyle
        {
            get
            {
                return this.encodingStyleField;
            }
            set
            {
                this.encodingStyleField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public partial class EnvelopeBody
    {
        private catalogProductListResponse catalogProductListResponseField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Magento")]
        public catalogProductListResponse catalogProductListResponse
        {
            get
            {
                return this.catalogProductListResponseField;
            }
            set
            {
                this.catalogProductListResponseField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Magento")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Magento", IsNullable = false)]
    public partial class catalogProductListResponse
    {
        private storeView storeViewField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("storeView")]
        public storeView storeView
        {
            get
            {
                return this.storeViewField;
            }
            set
            {
                this.storeViewField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntityArray")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class storeView
    {
        private storeViewItem[] itemField;

        private string arrayTypeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("item")]
        public storeViewItem[] item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
        public string arrayType
        {
            get
            {
                return this.arrayTypeField;
            }
            set
            {
                this.arrayTypeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntity")]
    public partial class storeViewItem
    {

        private ushort product_idField;

        private string skuField;

        private string nameField;

        private byte setField;

        private string typeField;

        private storeViewItemCategory_ids category_idsField;

        private storeViewItemWebsite_ids website_idsField;

        /// <remarks/>
        public ushort product_id
        {
            get
            {
                return this.product_idField;
            }
            set
            {
                this.product_idField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public string sku
        {
            get
            {
                return this.skuField;
            }
            set
            {
                this.skuField = value;
            }
        }

        /// <remarks/>
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        public byte set
        {
            get
            {
                return this.setField;
            }
            set
            {
                this.setField = value;
            }
        }

        /// <remarks/>
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        public storeViewItemCategory_ids category_ids
        {
            get
            {
                return this.category_idsField;
            }
            set
            {
                this.category_idsField = value;
            }
        }

        /// <remarks/>
        public storeViewItemWebsite_ids website_ids
        {
            get
            {
                return this.website_idsField;
            }
            set
            {
                this.website_idsField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class storeViewItemCategory_ids
    {

        private byte[] itemField;

        private string arrayTypeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("item")]
        public byte[] item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
        public string arrayType
        {
            get
            {
                return this.arrayTypeField;
            }
            set
            {
                this.arrayTypeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class storeViewItemWebsite_ids
    {

        private byte itemField;

        private string arrayTypeField;

        /// <remarks/>
        public byte item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
        public string arrayType
        {
            get
            {
                return this.arrayTypeField;
            }
            set
            {
                this.arrayTypeField = value;
            }
        }
    }
}

With the code

var deserializedDocument = SerializacaoHelper.Deserialize<ProductListResponse.Envelope>(xmlDocument);

public static T Deserialize<T>(XmlDocument xmlDoc)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));

        MemoryStream xmlStream = new MemoryStream();
        xmlDoc.Save(xmlStream);
        xmlStream.Position = 0;
        T obj = (T)serializer.Deserialize(xmlStream);

        xmlStream.Flush();

        return obj;
    }

The class was generated with Visual Studio. To simulate the creation of the class you must follow the steps in Edit > Paste Special > XML Paste as Classes with XML copied on clipboard.

The deserialize function is being executed, however, the attribute storeView is deserializes null even having child nodes inside it in XML.

Where is the error? In the class? In the code of deserialization? I can’t understand why the Storeview attribute deserializes null if in XML it has data.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.