将Soap XML解析为C#对象 [英] Parse Soap XML into Object in C#

查看:89
本文介绍了将Soap XML解析为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下SOAP XML

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

我的代码是:

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

推荐答案

您需要使您的类名称和属性名称与XML中的名称匹配-"GetUserProfileResponse" ,例如,"firstNameAr" ,请记住XML序列化区分大小写.或者,您需要使用控制XML序列化的属性将XML元素名称映射到您的类和属性名称.您还需要告诉 XmlSerializer ,您的类已在"http://schemas.abudhabi.ae/sso/2010/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.

对您有用的属性是:

  1. XmlRootAttribute -允许您指定从给定类型创建的根元素的名称及其名称空间.

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

XmlElementAttribute -允许您指定要序列化类型的成员的元素名称.

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

因此:

[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.
}

有关更多说明,请参见控制XML序列化使用属性.

For more instructions, see Controlling XML Serialization Using Attributes.

您还可以自动生成您的课程.有关说明,请参见从XML生成C#类.

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

这篇关于将Soap XML解析为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆