views:

68

answers:

2

I have an object which I serialize nicely into this:

<?xml version="1.0" encoding="utf-8" ?> 
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" userID="AX12345">
  <group groupID="1234_ABCD">
    <person name="Name 0" id="0" /> 
    <person name="Name 1" id="1" /> 
    <person name="Name 2" id="2" /> 
    <person name="Name 3" id="3" /> 
  </group>
</people>

Which is returned as a string to this:

    [OperationContract]
    [WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
    string Get(string format,string userid,string sessionkey);

When I view the returned data this service, I get this.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <s:Header />
  <s:Body>
    <GetResponse xmlns="http://tempuri.org/"&gt;
      <GetResult>**&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" userID="123BOBBY"&gt;&lt;group groupID="1234_ABCD"&gt;&lt;person name="Name 0" id="0" /&gt;&lt;person name="Name 1" id="1" /&gt;&lt;person name="Name 2" id="2" /&gt;&lt;person name="Name 3" id="3" /&gt;&lt;/group&gt;&lt;/people&gt;</**GetResult>
    </GetResponse>
  </s:Body>
</s:Envelope>

And what I would like is this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <s:Header />
  <s:Body>
    <GetResponse xmlns="http://tempuri.org/"&gt;
      <GetResult>
        <people userID="AX12345">
          <group groupID="1234_ABCD">
            <person name="Name 0" id="0" /> 
            <person name="Name 1" id="1" /> 
            <person name="Name 2" id="2" />
            <person name="Name 3" id="3" /> 
          </group>
        </people>
      </GetResult>
    </GetResponse>
  </s:Body>
</s:Envelope>

I am kind of a noob with all this, and I know I am missing something simple. Can anybody help me please?

Thanks P

+2  A: 

Instead of serializing the object you may try returning it directly from the method and leave WCF to handle serialization:

[OperationContract]
[WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
YourObject Get(string format,string userid,string sessionkey);
Darin Dimitrov
Hi - and thanks for responding so quickly.the reason I was trying to serialize is thet WCF wraps the returned object in it's own tagging scheme - it prefaces <a before all the data tags and also there seems to be no control of how a complex object properties are serialized, and no differentiation between XML attributes and elements.Thanks
Peter
Hmmm - I found this: http://msdn.microsoft.com/en-us/magazine/cc163569.aspxI'll check it out and post my findings - looks like it may work for me - 12 pages of bed-time reading ;)
Peter
A: 

It turns out the answer was fairly simple.

[XmlSerializerFormat]
[OperationContract]
[WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
string Get(string format,string userid,string sessionkey);

DataContractSerializer is the default serializer, and [XmlSerializerFormat] overrides the serialization.

In my classes I have added serialization attributes:

using System; using System.Collections.Generic; using System.Xml.Serialization;

namespace Myapp { [XmlRoot("people")] public class People { private string strUserID = ""; private List lstGroup;

    public People()
    {
       lstGroup = new List<Group>(); 
    }

    [XmlAttribute("userID")]
    public string UserID
    {
        get { return strUserID; }
        set { strUserID = value; }
    }
    [XmlElement("group")]
    public List<Group> Group
    {
        get { return lstGroup; }
        set { lstGroup = value; }
    }

 }

}

While I am happy with the short term gain, I will be looking deeper into this to make sure I keep control of the output.

Thanks

P

Peter