views:

62

answers:

2

I have the following classes in C# that I want to serialize:

Item

ProjectItem : Item

Folder : Item

Project (Contains a collection of Item, that may be either a ProjectItem or a Folder)

When I serialize a "Project", I get the following...

<Project>
  <Item xsi:type="Folder">
   <Name>MyFolder</Name> 
  </Item>
</Project>

While I'd like something like...

<Project>
  <Folder>
   <Name>MyFolder</Name> 
  </Folder>
</Project>

Do you have any idea about how to do that? Thanks in advance.

+2  A: 

I believe you need to add some attributes like this to your Project property:

[XmlArray]
[XmlArrayItem(ElementName="ProjectItem", Type=typeof(ProjectItem))]
[XmlArrayItem(ElementName="Folder", Type=typeof(Folder))]
public List<Item> Project
{
   get;
   set;
}
Jake Pearson
Worked perfectly. I appreciate your help.
Ciwee
+2  A: 

You can control naming by attributes:

public class MyEntity{
    [XmlElement(ElementName = "Folder")]
    public someType ProjectItem{get;set;};
}
Vitaliy Liptchinsky
Thank you for your answer, but it didn't completetely solved my problem. The "Project" property of type "Project" may contain either Folder or ProjectItem, so I can't assume it will always be "Folder" for instance.
Ciwee