Andre Pena
Andre Pena

Reputation: 59336

Question about XmlSerializer in .NET

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.

Upvotes: 1

Views: 123

Answers (2)

Vitaliy Liptchinsky
Vitaliy Liptchinsky

Reputation: 5299

You can control naming by attributes:

public class MyEntity{
    [XmlElement(ElementName = "Folder")]
    public someType ProjectItem{get;set;};
}

Upvotes: 2

Jake Pearson
Jake Pearson

Reputation: 27717

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;
}

Upvotes: 2

Related Questions