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
To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.
(SOAP is the specification that defines the guidelines used to describe remote procedure calls using XML.) XML provides the syntax needed to define a markup vocabulary—the tags and attributes needed to describe a particular type of data. XML files can be created using a simple text editor, such as Notepad.
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:
XmlRootAttribute
- allows you to specify the name of the root element created from a given type, as well as its namespace.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With