views:

163

answers:

1

The problem is that I' not getting the structure of XML that I want. My Code is as follows:

[DataContract]
public class Persons
{
    [DataMember]
    public List<Person> Personas;
}

[DataContract]
public class Person
{
    [DataMember(Name="SSN")]
    public long SSN
    {
        get;
        set;
    }
    [DataMember(Name="Name")]
    public string Name
    {
        get;
        set;
    }

And when I run the DataContractSerializer the XML it returns is:

<Persons xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WCFTest"&gt;
 <Personas>
  <Person>
   <Name>B</Name>
   <SSN>1234</SSN>
  </Person>
  <Person>
   <Name>I</Name>
   <SSN>5678</SSN>
  </Person>
 </Personas>
</Persons>

I woul like to eliminate either element Persons or Personas, the root element should contain a list of Person.

A: 

If you want the root element to be a list of Person, simply serialize the List instead of serializing Personas.

DataContractSerializer ser =
                new DataContractSerializer(typeof(List<Person>))
ser.WriteObject(writer, persons.Personas)

instead of

DataContractSerializer ser =
                new DataContractSerializer(typeof(Persons))
ser.WriteObject(writer, persons)

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

Tanzelax