locked
Deserialize to Class fails due to attributes RRS feed

  • Question

  • User439975351 posted

    Hi all,

    I have the joy of working with the DHL XML API. I am trying to map the response to a class but I get invalid xml "https://www.dhl.com" not found. Does anyone know how I can just ignore the attributes passed back or remove them to map this? Or any other tips to acheive the same? Heres the response from DHL on error:

    <?xml version="1.0" encoding="UTF-8"?><res:ErrorResponse xmlns:res='http://www.dhl.com' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation= 'http://www.dhl.com err-res.xsd'>
    <Response>
    <ServiceHeader>
    <MessageTime>2019-04-18T09:47:22.513+02:00</MessageTime>
    <MessageReference>bcf68330c65d49529dcadbafdda1b9a4</MessageReference>
    <SiteID></SiteID>
    <Password></Password>
    </ServiceHeader>
    <Status>
    <ActionStatus>Error</ActionStatus>
    <Condition>
    <ConditionCode>111</ConditionCode>
    <ConditionData>Error in parsing request XML:Error:
    Element type &quot;MetaData&quot; must be declared.
    at line 1, column 471</ConditionData>
    </Condition>
    </Status>
    </Response></res:ErrorResponse><!-- ServiceInvocationId:20190412194722_97ed_2b7e43e0-c0b5-4077-bdc3-b3d7f813979a -->

    Thursday, April 18, 2019 7:54 AM

Answers

  • User475983607 posted

    Use Visual Studio to generate the object model.  Copy the XML, in Visual Studio select Edit -> Paste Special -> Paste XML as Classes.

    Working example.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml.Serialization;
    using QC = System.Data.SqlClient;
    
    namespace ConsoleCs
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                string filename = @"data.xml";
                // Create an instance of the XmlSerializer.
                XmlSerializer serializer = new XmlSerializer(typeof(ErrorResponse));
    
                // Declare an object variable of the type to be deserialized.
                ErrorResponse i;
    
                using (Stream reader = new FileStream(filename, FileMode.Open))
                {
                    // Call the Deserialize method to restore the object's state.
                    i = (ErrorResponse)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(i.Response.ServiceHeader.MessageReference);
    
            }
        }
    
    
        // 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://www.dhl.com")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.dhl.com", IsNullable = false)]
        public partial class ErrorResponse
        {
    
            private Response responseField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
            public Response Response
            {
                get
                {
                    return this.responseField;
                }
                set
                {
                    this.responseField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Response
        {
    
            private ResponseServiceHeader serviceHeaderField;
    
            private ResponseStatus statusField;
    
            /// <remarks/>
            public ResponseServiceHeader ServiceHeader
            {
                get
                {
                    return this.serviceHeaderField;
                }
                set
                {
                    this.serviceHeaderField = value;
                }
            }
    
            /// <remarks/>
            public ResponseStatus Status
            {
                get
                {
                    return this.statusField;
                }
                set
                {
                    this.statusField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseServiceHeader
        {
    
            private System.DateTime messageTimeField;
    
            private string messageReferenceField;
    
            private object siteIDField;
    
            private object passwordField;
    
            /// <remarks/>
            public System.DateTime MessageTime
            {
                get
                {
                    return this.messageTimeField;
                }
                set
                {
                    this.messageTimeField = value;
                }
            }
    
            /// <remarks/>
            public string MessageReference
            {
                get
                {
                    return this.messageReferenceField;
                }
                set
                {
                    this.messageReferenceField = value;
                }
            }
    
            /// <remarks/>
            public object SiteID
            {
                get
                {
                    return this.siteIDField;
                }
                set
                {
                    this.siteIDField = value;
                }
            }
    
            /// <remarks/>
            public object Password
            {
                get
                {
                    return this.passwordField;
                }
                set
                {
                    this.passwordField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseStatus
        {
    
            private string actionStatusField;
    
            private ResponseStatusCondition conditionField;
    
            /// <remarks/>
            public string ActionStatus
            {
                get
                {
                    return this.actionStatusField;
                }
                set
                {
                    this.actionStatusField = value;
                }
            }
    
            /// <remarks/>
            public ResponseStatusCondition Condition
            {
                get
                {
                    return this.conditionField;
                }
                set
                {
                    this.conditionField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseStatusCondition
        {
    
            private byte conditionCodeField;
    
            private string conditionDataField;
    
            /// <remarks/>
            public byte ConditionCode
            {
                get
                {
                    return this.conditionCodeField;
                }
                set
                {
                    this.conditionCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ConditionData
            {
                get
                {
                    return this.conditionDataField;
                }
                set
                {
                    this.conditionDataField = value;
                }
            }
        }
    
    
    }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2019 11:18 AM

All replies

  • User475983607 posted

    Use Visual Studio to generate the object model.  Copy the XML, in Visual Studio select Edit -> Paste Special -> Paste XML as Classes.

    Working example.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml.Serialization;
    using QC = System.Data.SqlClient;
    
    namespace ConsoleCs
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                string filename = @"data.xml";
                // Create an instance of the XmlSerializer.
                XmlSerializer serializer = new XmlSerializer(typeof(ErrorResponse));
    
                // Declare an object variable of the type to be deserialized.
                ErrorResponse i;
    
                using (Stream reader = new FileStream(filename, FileMode.Open))
                {
                    // Call the Deserialize method to restore the object's state.
                    i = (ErrorResponse)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(i.Response.ServiceHeader.MessageReference);
    
            }
        }
    
    
        // 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://www.dhl.com")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.dhl.com", IsNullable = false)]
        public partial class ErrorResponse
        {
    
            private Response responseField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
            public Response Response
            {
                get
                {
                    return this.responseField;
                }
                set
                {
                    this.responseField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Response
        {
    
            private ResponseServiceHeader serviceHeaderField;
    
            private ResponseStatus statusField;
    
            /// <remarks/>
            public ResponseServiceHeader ServiceHeader
            {
                get
                {
                    return this.serviceHeaderField;
                }
                set
                {
                    this.serviceHeaderField = value;
                }
            }
    
            /// <remarks/>
            public ResponseStatus Status
            {
                get
                {
                    return this.statusField;
                }
                set
                {
                    this.statusField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseServiceHeader
        {
    
            private System.DateTime messageTimeField;
    
            private string messageReferenceField;
    
            private object siteIDField;
    
            private object passwordField;
    
            /// <remarks/>
            public System.DateTime MessageTime
            {
                get
                {
                    return this.messageTimeField;
                }
                set
                {
                    this.messageTimeField = value;
                }
            }
    
            /// <remarks/>
            public string MessageReference
            {
                get
                {
                    return this.messageReferenceField;
                }
                set
                {
                    this.messageReferenceField = value;
                }
            }
    
            /// <remarks/>
            public object SiteID
            {
                get
                {
                    return this.siteIDField;
                }
                set
                {
                    this.siteIDField = value;
                }
            }
    
            /// <remarks/>
            public object Password
            {
                get
                {
                    return this.passwordField;
                }
                set
                {
                    this.passwordField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseStatus
        {
    
            private string actionStatusField;
    
            private ResponseStatusCondition conditionField;
    
            /// <remarks/>
            public string ActionStatus
            {
                get
                {
                    return this.actionStatusField;
                }
                set
                {
                    this.actionStatusField = value;
                }
            }
    
            /// <remarks/>
            public ResponseStatusCondition Condition
            {
                get
                {
                    return this.conditionField;
                }
                set
                {
                    this.conditionField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class ResponseStatusCondition
        {
    
            private byte conditionCodeField;
    
            private string conditionDataField;
    
            /// <remarks/>
            public byte ConditionCode
            {
                get
                {
                    return this.conditionCodeField;
                }
                set
                {
                    this.conditionCodeField = value;
                }
            }
    
            /// <remarks/>
            public string ConditionData
            {
                get
                {
                    return this.conditionDataField;
                }
                set
                {
                    this.conditionDataField = value;
                }
            }
        }
    
    
    }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2019 11:18 AM
  • User439975351 posted

    Perfect, thanks mgebhard for your help :)

    Thursday, April 18, 2019 2:05 PM