tags:

views:

229

answers:

1

I have an object created with the xsd.exe tool that defines xml attributes in the code, but the SOAP response from my web service is returning xmlelements instead of attributes.

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Accountinfo {

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string UpdatedDate;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string UpdatedBy;

... etc

As you can see, UpdatedDate, etc are defined as attributes. When I call my service, the soap body I get back returns the Accountinfo element as such:

<a:Accountinfo> <a:UpdatedBy>IGD</a:UpdatedBy> <a:UpdatedDate>12/18/2009 9:43:06 AM</a:UpdatedDate> ... etc

What I'm looking for is <AccountInfo UpdatedBy="IGD" UpdatedDate="12/18/2009 9:43:06 AM" ... />

I don't have a lot of experience with XML, SOAP, or WCF, but I'm using all three now and need to get this working. What am I missing here?

+2  A: 

Does your WCF SOAP webservice use the standard WCF DataContractSerializer? If so - that's expected behavior.

For the sake of about 10% more speed, the DataContractSerializer (which is used by default in WCF, unless you explicitly ask for the XmlSerializer) forgoes XML attributes and serializes everything as elements.

Even having all those [XmlAttribute] attributes on your data doesn't make a difference - you need to specifically ask for the XmlSerializer, either when creating your proxy client (using svcutil.exe or the Visual Studio "Add Service Reference" dialog), or by specifying the [XmlSerializerFormat] attribute on your service implementation.

Read more about the XmlSerializerFormat attribute and see Dan Rigsby's excellent comparison of the WCF DataContractSerializer vs. XmlSerializer

marc_s