views:

30

answers:

1

I am trying to control the Xml output during a Wcf Rest serialisation process. I want to lose a tier in the output hierarchy. (I.e. lose the <content> tags). I have looked through the various attribute settings available, but not managed to find what I'm looking for. Can anyone help?


My output is currently:

<?xml version="1.0" encoding="utf-8" ?>
<ContentStructure xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <content>
    <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
      <title><![CDATA[Content Title 1]]></title>
    </content_item>
    <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
      <title><![CDATA[Content Title 1]]></title>
    </content_item>
    <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
      <title><![CDATA[Content Title 1]]></title>
    </content_item>
  </content>
</ContentStructure>

What I am trying to get is:

<?xml version="1.0" encoding="utf-8" ?>
<ContentStructure xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
    <title><![CDATA[Content Title 1]]></title>
  </content_item>
  <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
    <title><![CDATA[Content Title 1]]></title>
  </content_item>
  <content_item id="{BED36077-DECE-4FCA-BE8F-D6D450B6CC08}">
    <title><![CDATA[Content Title 1]]></title>
  </content_item>
</ContentStructure>

My class structure is as follows:

[DataContract(Namespace = "")]  
public class ContentStructure  
{  
    [DataMember(Order = 0)]  
    [XmlArrayItem(ElementName = "content_item")]  
    public List<ContentItem> content { get; set; }  
}  

Where ContentItem contains a set of fields.

My wcf service is defined as:

[WebHelp(Comment = "Returns content xml")]
[WebGet(UriTemplate = "/Content/Gallery", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract(Name = "GetContent")]
[XmlSerializerFormat]
public ContentStructure GetContent()
{
    return ServiceProvider.GetContent();
}

My contract is:

public interface IServiceProvider
{
    ContentStructure GetContent();
}

Which is implemented as:

public class ServiceProvider : IServiceProvider
{
    public ContentStructure GetContent()
    {
        return GetContentItems(CONTENT_ROOT);
    }
}

Thanks in advance.

A: 

The solution to this was quite simple and I thought I had tried it before posting the question.

Replacing the XmlArrayItem attribute with the [XmlElement("content_item")] attribute on the Content property of the ContentStructure class does the trick.

[DataContract(Namespace = "")]   
public class ContentStructure   
{   
    [DataMember(Order = 0)]   
    [XmlElement("content_item")]   
    public List<ContentItem> Content { get; set; }   
} 
Orbis