简体   繁体   中英

Deserialize SOAP XML Response

I'm having trouble figuring out a way to deserialize an XML response from a web service. The response comes in this format:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<providerDemographicsResponse xmlns="http://provapi.sys.test.com/provider/network/messages/providerDemographicsResponse/v1" xmlns:ns2="http://provapi.sys.test.com/provider/network/messages/providerAddress/v1" xmlns:ns3="http://provapi.sys.test.com/provider/network/messages/expandedProvider/v1" xmlns:ns4="http://provapi.sys.test.com/provider/network/messages/enrollmentDetails/v1" xmlns:ns5="http://provapi.sys.test.com/provider/network/messages/providerBusinessEntity/v1" xmlns:ns6="http://provapi.sys.test.com/provider/network/messages/providerNpoAssociation/v1" xmlns:ns7="http://provapi.sys.test.com/provider/network/messages/serviceAreaDetail/v1" xmlns:ns8="http://provapi.sys.test.com/provider/network/messages/networkProviderAddress/v1" xmlns:ns9="http://provapi.sys.test.com/provider/network/messages/delegationEntity/v1" xmlns:ns10="http://provapi.sys.test.com/provider/network/messages/providerSpecialty/v1" xmlns:ns11="http://provapi.sys.test.com/provider/network/messages/providerNpi/v1" xmlns:ns12="http://provapi.sys.test.com/provider/common/messages/metadata/v1">
  <ns12:metadata>
    <ns12:serviceReferenceId>test17-02-2016 16:05:47.000616</ns12:serviceReferenceId>
    <ns12:limit>1</ns12:limit>
    <ns12:offset>0</ns12:offset>
    <ns12:total>1</ns12:total>
    <ns12:outcome>
      <ns12:status>200</ns12:status>
      <ns12:message>Successful.</ns12:message>
      <ns12:code>200</ns12:code>
      <ns12:additionalDetails/>
    </ns12:outcome>
  </ns12:metadata>
  <data>
    <providerDemographics>
      <cpfProviderId>0000010</cpfProviderId>
      <effectiveDate>1980-01-01</effectiveDate>
      <terminationDate>9999-12-31</terminationDate>
      <provider-under-review-indicator>N</provider-under-review-indicator>
      <providerTypeDescription>Healthcare Organization</providerTypeDescription>
    </providerDemographics>
    <providerDemographics>
      <cpfProviderId>0000010</cpfProviderId>
      <effectiveDate>1980-01-01</effectiveDate>
      <terminationDate>9999-12-31</terminationDate>
      <provider-under-review-indicator>N</provider-under-review-indicator>
      <providerTypeDescription>Healthcare Organization</providerTypeDescription>
    </providerDemographics>
  </data>
</providerDemographicsResponse>

I have this class to get at the providerDemographics list at the bottom of the XML:

public class ProviderDemographics {

    [XmlAttribute(AttributeName = "cpfProviderId")]
    public int CpfProviderId { get; set; }

    [XmlAttribute(AttributeName = "effectiveDate")]
    public DateTime EffectiveDate { get; set; }

    [XmlAttribute(AttributeName = "terminationDate")]
    public DateTime TerminationDate { get; set; }

    [XmlAttribute(AttributeName = "provider-under-review-indicator")]
    public string ProviderUnderReviewIndicator { get; set; }

    [XmlAttribute(AttributeName = "providerTypeDescription")]
    public string ProviderTypeDescription { get; set; }

}

I tried using the regular XmlSerializer to deserialize it, but I get an exception (I think relating to the number of namespaces, but I'm not entirely sure). I also tried using Linq, but when I try parising the XDocument, I get an empty list.

XDocument doc = XDocument.Parse(xml);

var list = (from d in doc.Descendants("providerDemographics")
            select new ProviderDemographics {
                CpfProviderId = (int)d.Attribute("cpfProviderId")
            }).ToList();

Any idea how I can extract the providerDemographics as a list from the reponse?

You need to add the namespace when you are searching the elements. Because your wanted element doesn't have any specific namespace you should take the parent element namespace. In this case it is namespace of providerDemographicsResponse

Here is working example:

        static void Main(string[] args)
        {
            string xml = "your provided Xml"

            XDocument doc = XDocument.Parse(xml);

            XNamespace metadataNameSpace = "http://provapi.sys.test.com/provider/common/messages/metadata/v1";
            XNamespace headNameSpace = "http://provapi.sys.test.com/provider/network/messages/providerDemographicsResponse/v1";

            //if you want tags providerDemographics
            var list = (from d in doc.Descendants(headNameSpace + "providerDemographics")
                        select new
                        {
                            CpfProviderId  = d.Element(headNameSpace + "cpfProviderId").Value
                        }).ToList();

            //if you want metadata tags data   
            var metaDatalist = (from d in doc.Descendants(metadataNameSpace + "metadata")
                                select d).ToList();

            Console.ReadKey();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM