I need to serialice an object to XML. I'm stuck on this property, which is a list of objects, where I need the wrapper tag of the list, but not the wrapper tag of the items in the list.
My code:
public class Partner
{
//[XmlElement("PartnerContact")] //This will remove the wrapper tag, not the item tags whitin the list tag
public List<PartnerContact> PartnerContacts { get; set; } = new List<PartnerContact> { };
}
public class PartnerContact
{
public string ContactType { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
}
What I have (whitout using the XmlElement attribute):
<Partner>
<PartnerContacts>
<PartnerContact>
<ContactType>AR</ContactType>
<Name>John Doe</Name>
<PhoneNumber>0123456789</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
</PartnerContact>
<PartnerContact>
<ContactType>OR</ContactType>
<Name>John Does wife</Name>
<PhoneNumber>987654321</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
</PartnerContact>
</PartnerContacts>
</Partner>
What I need:
<Partner>
<PartnerContacts>
<ContactType>AR</ContactType>
<Name>John Doe</Name>
<PhoneNumber>0123456789</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<ContactType>OR</ContactType>
<Name>John Does wife</Name>
<PhoneNumber>987654321</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
</PartnerContacts>
</Partner>
I know that I can set an XmlElement attribute for the list property to remove the wrapper tag, but that's not what I need here.
Can someone help me out with this?
CodePudding user response:
I tried several things, including the use of XmlElementAttribute
and XmlChoiceIdentifierAttribute
, but it did not give me the outcome I needed.
In the end I came up with the following solution:
My code:
public class Document
{
public List<Partner> Partners { get; set; } = new List<Partner> { };
}
public class Partner
{
public string PartnerType { get; set; }
public PartnerContacts PartnerContacts { get; set; } = new PartnerContacts();
}
public class PartnerContacts
{
[XmlElement("ContactType", Type = typeof(ContactType))]
[XmlElement("Name", Type = typeof(Name))]
[XmlElement("PhoneNumber", Type = typeof(PhoneNumber))]
[XmlElement("EmailAddress", Type = typeof(EmailAddress))]
public List<PartnerContact> PartnerContactsInfos { get; set; } = new List<PartnerContact>();
}
public abstract class PartnerContact
{
[XmlText]
public string Info { get; set; }
}
public sealed class ContactType : PartnerContact
{
}
public sealed class Name : PartnerContact
{
}
public sealed class PhoneNumber : PartnerContact
{
}
public sealed class EmailAddress : PartnerContact
{
}
This gets me the result I wanted:
<Document>
<Partners>
<Partner>
<PartnerID>TestID</PartnerID>
<PartnerContacts>
<ContactType>AR</ContactType>
<Name>John Doe</Name>
<PhoneNumber>0123456789</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<ContactType>OR</ContactType>
<Name>John Does Wife</Name>
<PhoneNumber>9876543210</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
</PartnerContacts>
</Partner>
</Partners>
</Document>