开发者

WCF Web Service Response XML file

I am using a data contract in WCF, but there is some unwanted response tags being sent in the xml file, I am not sure how to modify the data contract class so that I can avoid sending that unwanted response field. In the below XML file, all the text which is bolded and italics is unwanted. Thanks.

  <GetCertMailReceiptNumbersResponse xmlns="http://tempuri.org/">
      <GetCertMailReceiptNumbersResult xmlns:a="http://schemas.datacontract.org/2004/07/WcfSmartConnect" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        ***<a:AccountNumber i:nil="true" />
        <a:Address1 i:nil="true" />
        <a:Address2 i:nil="true" />
        <a:City i:nil="true" />
        <a:Name1 i:nil="true" />
        <a:Name2 i:nil="true" />
        <a:OrderNumber>1325661</a:OrderNumber>
        <a:Password i:nil="true" />***
        <a:ReturnCode>200</a:ReturnCode>
        <a:ReturnMessage />
        ***<a:SoftwareID i:nil="true" />
        <a:State i:nil="true" />
        <a:TrackingStatus i:nil="true" />
        <a:USPSReceipt i:nil="true" />
        <a:UserID i:nil="true" />
        <a:Zip i:nil="true" />***
        <a:lsCMRN>
          <a:CertMailReceiptNumbers>
            <a:AccountNumber>1</a:AccountNumber>
            <a:Address1>201 Test Ln</a:Address1>
            <a:Address2>TSTCity TST  123456</a:Address2>
            <a:City>TSTCity</a:City>
            <a:Name1>pinu</a:Name1>
            <a:Name2 i:nil="true" />
            <a:OrderNumber />
            ***<a:Password />
            <a:ReturnCode>0</a:ReturnCode>
            <a:ReturnMessage i:nil="true" />
            <a:SoftwareID />***
            <a:State> TST </a:State>
            <a:TrackingStatus>MAILED</a:TrackingStatus>
            <a:USPSReceipt>71926389001019</a:USPSReceipt>
            ***<a:UserID />***
            <a:Zip>123456</a:Zip>
            <a:lsCMRN />
            <a:order>1325661</a:order>
          </a:CertMailReceiptNumbers>
          <a:CertMailReceiptNumbers>
            <a:AccountNumber>10</a:AccountNumber>
            <a:Address1>121 Mant Drive</a:Address1>
            <a:Address2>TestCity  Test  123456</a:Address2>
            <a:City> TestCity </a:City>
            <a:Name1>Kris </a:Name1>
            <a:Name2 i:nil="true" />
            <a:OrderNumber />
            ***<a:Password />
            <a:ReturnCode>0</a:ReturnCode>
            <a:ReturnMessage i:nil="true" />
            <a:SoftwareID />***
            <a:State>Test</a:State>
            <a:TrackingStatus>MAILED</a:TrackingStatus>
            <a:USPSReceipt>71926389000026</a:USPSReceipt>
            ***<a:UserID />***
            <a:Zip>123456</</a:Zip>
            <a:lsCMRN />
            <a:order>1325661</a:order>
          </a:CertMailReceiptNumbers>
          <a:CertMailReceiptNumbers>
            <a:AccountNumber>2</a:AccountNumber>
            <a:Address1>121 Technot</a:Address1>
            <a:Address2>Test  Tst  123456</a:Address2>
            <a:City>Test</a:City>
            <a:Name1>Alice</a:Name1>
            <a:Name2 i:nil="true" />
            <a:OrderNumber />
            ***<a:Password />
            <a:ReturnCode>0</a:ReturnCode>
            <a:ReturnMessage i:nil="true" />
            <a:SoftwareID />***
            <a:State>Tst</a:State>
            <a:TrackingStatus>MAILED</a:TrackingStatus>
            <a:USPSReceipt>7192638900000033</a:USPSReceipt>
            ***<a:UserI开发者_如何学JAVAD />***
            <a:Zip>123456</</a:Zip>
            <a:lsCMRN />
            <a:order>1325661</a:order>
          </a:CertMailReceiptNumbers>
          <a:CertMailReceiptNumbers>
            <a:AccountNumber>3</a:AccountNumber>
            <a:Address1>194 Green st</a:Address1>
            <a:Address2>Matt  MA  123456</a:Address2>
            <a:City>Matt</a:City>
            <a:Name1>Jewel Bondurant</a:Name1>
            <a:Name2 i:nil="true" />
            <a:OrderNumber />
            ***<a:Password />
            <a:ReturnCode>0</a:ReturnCode>
            <a:ReturnMessage i:nil="true" />
            <a:SoftwareID />***
            <a:State>MA</a:State>
            <a:TrackingStatus>MAILED</a:TrackingStatus>
            <a:USPSReceipt>71926389001000000040</a:USPSReceipt>
            ***<a:UserID />***
            <a:Zip>123456</</a:Zip>
            <a:lsCMRN />
            <a:order>1325661</a:order>
          </a:CertMailReceiptNumbers>
          <a:CertMailReceiptNumbers>
            <a:AccountNumber>4</a:AccountNumber>
            <a:Address1>123  Nasas</a:Address1>
            <a:Address2>Mtar  Tst  123456</a:Address2>
            <a:City>Mtar</a:City>
            <a:Name1>Terry </a:Name1>
            <a:Name2 i:nil="true" />
            ***<a:OrderNumber />
            <a:Password />
            <a:ReturnCode>0</a:ReturnCode>
            <a:ReturnMessage i:nil="true" />
            <a:SoftwareID />***
            <a:State>Tst</a:State>
            <a:TrackingStatus>MAILED</a:TrackingStatus>
           a:USPSReceipt>7192630000057</a:USPSReceipt>
            ***<a:UserID />***
            <a:Zip>123456</</a:Zip>
            <a:lsCMRN />
            <a:order>1325661</a:order>
          </a:CertMailReceiptNumbers>
        </a:lsCMRN>
        <a:order>0</a:order>
      </GetCertMailReceiptNumbersResult>
    </GetCertMailReceiptNumbersResponse>


Show us your classes that are being sent back as response from your WCF service! Without them, it's at best a guessing game....

You can put certain DataContract-related attributes on properties to include or exclude them from the serialization process in WCF.

Basically, with WCF in .NET 3.5 SP1, you don't have to specifically label your classes and their properties with special attributes anymore. If you don't, the DataContract serializer will act like the XML serializer : all public properties are being serialized.

If you don't want to include certain properties in the serialization, you don't really have a choice but to

  • put the [DataContract] attribute on your classes, and
  • put the [DataMember] attribute on your fields and properties that you do want to be serialized

Anything you do not adorn with a [DataMember] attribute will then be skipped and not included in the WCF response message.

So in your case, you would have to do something like

[DataContract]
class GetCertMailReceiptNumbersResult
{
    // no DataMember attribute --> will not be included!
    int AccountNumber {get;set;}
    string Address1 {get;set;}
   ......
    string Password {get;set;}

    // here, include DataMember attributes --> will be included
    [DataMember]
    int ReturnCode {get;set;}
    [DataMember]
    string ReturnMessage {get;set;}

    // and so forth
 }

Basically, for each type in your WCF service, you need to make a decision which fields and properties to include - mark those with the [DataMember] attribute. Anything else will not be included in your serialized message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜