2

How do I get the SOAP xml attribute value in a wcf service?

  <ns3:NotifRQ Status="Commit" 
               xmlns:ns2="http://www.dddd.com/df/dd/" 
               xmlns:ns3="http://www.dd.org/OTA/">

      <ns3:rev>dfdfkkl</ns3:rev>
      <ns3:change>dfdfkkl</ns3:change>
  </ns3:NotifRQ>

This is the code I have now for the data contract:

[DataContract(Name = "NotifRQ", Namespace = "http://www.dd.org/OTA/")]
public class NotifRQ
{
    [DataMember(Name = "Status")]
    public string ResStatus;
}
Mari
  • 237
  • 1
  • 4
  • 17

1 Answers1

0

Your Status attribute needs to be a field or property of the NotifRQ class and you need to instruct WCF to use the less optimal XmlSerializer instead of the DatacontractSerializer as explained here. You achieve that by using the XmlSerializerFormat attribute on your class.

You can now apply XmlAttribute to a field or property of your class that gets or sets the value of an attribute on the xml element.

Create and annotate your class as follows:

[DataContract(Namespace="http://www.dd.org/OTA/")]
[XmlSerializerFormat]
public class NotifRQ 
{

   [DataMember, XmlAttribute] 
   public string Status="Commit";

   [DataMember]
   public string  rev;

   [DataMember]
   public string  change;
}

Above class will write and read the following wire-format:

<?xml version="1.0" encoding="utf-16"?>
<NotifRQ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         Status="Commit">
  <rev>foo</rev>
</NotifRQ>
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152