0

I have the following class:

[Serializable]
public class SomeModel
{
    [XmlElement("CustomerName")]
    public string CustomerName { get; set; }

    [XmlElement("")]
    public int CustomerAge { get; set; }
}

Which (when populated with some test data) and Serialized using XmlSerializer.Serialize() results in the following XML:

<SomeModel>
  <CustomerName>John</CustomerName>
  <CustomerAge>55</CustomerAge>
</SomeModel>

What I need to have is:

<SomeModel>
  <CustomerName>John</CustomerName>
   55
</SomeModel>

Meaning for the 2nd xmlelement, it should not have its own tag. Is this even possible? Thank you.

Lor Kam Hoi
  • 59
  • 1
  • 8

1 Answers1

4

Decorate CustomerAge with XmlText instead of XmlElement.

You'll also have to change the type of CustomerAge from int to string and If you don't want to, you'll have to take an extra property for serialization like this:

public class SomeModel
{
    [XmlElement("CustomerName")]
    public string CustomerName { get; set; }

    [XmlText]
    public string CustomerAgeString { get { return CustomerAge.ToString(); } set { throw new NotSupportedException("Setting the CustomerAgeString property is not supported"); } }

    [XmlIgnore]
    public string CustomerAge { get; set; }
}
sachin
  • 2,341
  • 12
  • 24