tags:

views:

23

answers:

1

I am using WCF REST Start Kit for Visual Studio 2008 or .Net 3.5. The example service will be able to get clean xml result like this:

<SampleItem>
  <Value>SampleValue</Value>
</SampleItem>

The SampleItem is a simple class with Value property in Service.scv.cs. Then I tried to use a customized class like

[global::System.Serializable()]
public class MyDomainClass {
   [global::System.Runtime.Serialization.DataMemberAttribute()]
   public string Name { get; set; }
   [global::System.Runtime.Serialization.DataMemberAttribute()]
   public double Value { get; set; }
   ...
}

Then I use this class to plug in to replace Service.svc.cs' generic class:

 //public class Service : SingletonServiceBase<SampleItem>,
 //     ISingletonService<SampleItem>
 public class Service : SingletonServiceBase<MyDomainClass>,
      ISingletonService<MyDomainClass> {...}

It works fine but the XML result will look like:

<MyDomainClass>
   <_x003C_Name_x003E_k__BackingField>a name</_x003C_Name_x003E_k__BackingField>
   <_x003C_Value_x003E_k__BackingField>7.198</_x003C_Value_x003E_k__BackingField>
   ...

I think I have define a schema for my class so that XML might be clean like the example one. What I should do to get up my XML result in a clean or my expected way?

+1  A: 

The easiest way to clean up the names of your xml entities is to specify the name property of the DataMemberAttribute:

[Serializable, DataContract]    
public class MyDomainClass 
{
   [DataMember(Name="Name")]
   public string Name { get; set; }

   [DataMember(Name="Value")]
   public double Value { get; set; }
   ...
}
Rob Rodi
Yes, it works. What are other ways? from respond to build a string of object? That would be hard. Thanks!
David.Chu.ca