11

I have the following SOAP XML

string soap = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns='http://schemas.abudhabi.ae/sso/2010/11'>" +
   "<soapenv:Header/>" +
   "<soapenv:Body>" +
      "<ns:GetUserProfileResponse>" +
         "<!--Optional:-->" +
         "<ns:userid>?</ns:userid>" +
         "<!--Optional:-->" +
         "<ns:firstNameAr>?</ns:firstNameAr>" +
         "<!--Optional:-->" +
         "<ns:firstNameEn>?</ns:firstNameEn>" +
         "<!--Optional:-->" +
         "<ns:middleNameAr>?</ns:middleNameAr>" +
         "<!--Optional:-->" +
         "<ns:middleNameEn>?</ns:middleNameEn>" +
         "<!--Optional:-->" +
         "<ns:thirdNameAr>?</ns:thirdNameAr>" +
         "<!--Optional:-->" +
         "<ns:thirdNameEn>?</ns:thirdNameEn>" +
         "<!--Optional:-->" +
         "<ns:fourthNameAr>?</ns:fourthNameAr>" +
         "<!--Optional:-->" +
         "<ns:fourthNameEn>?</ns:fourthNameEn>" +
         "<!--Optional:-->" +
         "<ns:familyNameAr>?</ns:familyNameAr>" +
         "<!--Optional:-->" +
         "<ns:familyNameEn>?</ns:familyNameEn>" +
         "<!--Optional:-->" +
         "<ns:authLevel>?</ns:authLevel>" +
         "<!--Optional:-->" +
         "<ns:dateOfBirth>?</ns:dateOfBirth>" +
         "<!--Optional:-->" +
         "<ns:gender>?</ns:gender>" +
         "<!--Optional:-->" +
         "<ns:nationalityCode>?</ns:nationalityCode>" +
         "<!--Optional:-->" +
         "<ns:idn>?</ns:idn>" +
         "<!--Optional:-->" +
         "<ns:modifyTimestamp>?</ns:modifyTimestamp>" +
         "<!--Optional:-->" +
         "<ns:prefComChannel>?</ns:prefComChannel>" +
         "<!--Optional:-->" +
         "<ns:secretQuestionAnswer>?</ns:secretQuestionAnswer>" +
         "<!--Optional:-->" +
         "<ns:secretQuestionId>?</ns:secretQuestionId>" +
         "<!--Optional:-->" +
         "<ns:address>" +
            "<!--Optional:-->" +
            "<ns:additionalAddressInfo>?</ns:additionalAddressInfo>" +
            "<!--Optional:-->" +
            "<ns:city>?</ns:city>" +
            "<!--Optional:-->" +
            "<ns:fax>?</ns:fax>" +
            "<!--Optional:-->" +
            "<ns:residenceCountry>?</ns:residenceCountry>" +
            "<!--Optional:-->" +
            "<ns:poBox>?</ns:poBox>" +
            "<!--Optional:-->" +
            "<ns:stateOrEmirate>?</ns:stateOrEmirate>" +
            "<!--Optional:-->" +
            "<ns:streetNameAndNumber>?</ns:streetNameAndNumber>" +
            "<!--Optional:-->" +
            "<ns:zipCode>?</ns:zipCode>" +
         "</ns:address>" +
         "<!--Optional:-->" +
         "<ns:contact>" +
            "<!--Optional:-->" +
            "<ns:email>?</ns:email>" +
            "<!--Optional:-->" +
            "<ns:mobilePhoneNumber>?</ns:mobilePhoneNumber>" +
            "<!--Optional:-->" +
            "<ns:website>?</ns:website>" +
            "<!--Optional:-->" +
            "<ns:workPhone>?</ns:workPhone>" +
         "</ns:contact>" +
      "</ns:GetUserProfileResponse>" +
   "</soapenv:Body>" +
"</soapenv:Envelope>";

I wanted it to Parse or Convert into following Class

public class UserProfile
        {
            public string FirstNameAR { get; set; }

            public string FirstNameEN { get; set; }

            public string MiddleNameAR { get; set; }

            public string MiddleNameEN { get; set; }

            public string ThirdNameAR { get; set; }
            public string ThirdNameEN { get; set; }


            public string FourthNameAR { get; set; }

            public string FourthNameEN { get; set; }

            public string FamilyNameAR { get; set; }

            public string FamilyNameEN { get; set; }

            public Boolean AuthLevelSpecified { get; set; }

            public DateTime DateOfBirth { get; set; }
            public bool DateOfBirthSpecified { get; set; }


            public Boolean GenderTypeSpecified { get; set; }

            public string NationalityCode { get; set; }

            public string IDN { get; set; }

            public Boolean ModifyTimeStampSpecified { get; set; }
            public DateTime ModifyTimeStamp { get; set; }

            //  public PrefComChannelType PrefComChannelType { get; set; }
            public Boolean PrefComChannelTypeSpecified { get; set; }

            public string SecretQuestion { get; set; }

            public int SecretQuestionId { get; set; }

            public Boolean SecretQuestionSpecified { get; set; }
           }

and my code is :

var Value = XDocument.Parse(soap);

XNamespace ns = @"http://schemas.xmlsoap.org/soap/envelope/";
 var unwrappedResponse = Value.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body").First().FirstNode;

 XmlSerializer oXmlSerializer = new XmlSerializer(typeof(UserProfile));

 var responseObj = (UserProfile)oXmlSerializer.Deserialize(unwrappedResponse.CreateReader());

I am following this question removing/extracting soap header and body from soap message But I am getting error.Is there any thing wrong with this Code.Please Help

Community
  • 1
  • 1
BASEER HAIDER JAFRI
  • 939
  • 1
  • 17
  • 35

1 Answers1

11

You need to make your class name and property names match the names in the XML -- "GetUserProfileResponse" and, e.g., "firstNameAr", keeping in mind that XML serialization is case-sensitive. Alternatively, you need to use attributes that control XML serialization to map the XML element names to your class and property names. You also need to tell the XmlSerializer that your class is serialized in the "http://schemas.abudhabi.ae/sso/2010/11" namespace.

The attributes that are useful to you are:

  1. XmlRootAttribute - allows you to specify the name of the root element created from a given type, as well as its namespace.

  2. XmlElementAttribute - allows you to specify the element names of members of a type being serialized.

Thus:

[XmlRoot("GetUserProfileResponse", Namespace = "http://schemas.abudhabi.ae/sso/2010/11")] // Serialized with root element name "GetUserProfileResponse" in namespace "http://schemas.abudhabi.ae/sso/2010/11".
public class UserProfile
{
    [XmlElement("firstNameAr")] // Serialized with element name "firstNameAr".
    public string FirstNameAR { get; set; }

    [XmlElement("firstNameEn")]
    public string FirstNameEN { get; set; }

    [XmlElement("middleNameAr")]
    public string MiddleNameAR { get; set; }

    [XmlElement("middleNameEn")]
    public string MiddleNameEN { get; set; }

    [XmlElement("thirdNameAr")]
    public string ThirdNameAR { get; set; }

    [XmlElement("thirdNameEn")]
    public string ThirdNameEN { get; set; }

    // Fix others similarly.
}

For more instructions, see Controlling XML Serialization Using Attributes.

You could also auto-generate your classes. See Generate C# class from XML for instructions.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • I followed this but I am getting another error, Sequence contains no elements, can someone please help – N Khan Feb 26 '20 at 06:36
  • @NKhan - sorry this isn't working for you. If you need additional help, you should [ask](https://stackoverflow.com/questions/ask) a new question, linking back to this one and providing a [mcve]. I can't guess the problem with just the exception message. – dbc Feb 26 '20 at 22:39