Custom object XML serialization whithout any xml tags

32 views Asked by At

I would like to create a custom serializable "string" class to handle the default string formatting without implement the logic in all of the modells.

I created a serializable class with implicit string operator

[Serializable]
public class FormattedString : ISerializable
{

    public static implicit operator FormattedString(string s)
    {
        return new FormattedString() { OriginalString = s };
    }

    public string OriginalString { get; set; }

    public override string ToString()
    {
        if (string.IsNullOrWhiteSpace(this.OriginalString))
            this.OriginalString = String.Empty;

        this.OriginalString = OriginalString.Trim().Replace(Environment.NewLine, " ");
        return this.OriginalString;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("string", this.ToString());
    }
}

My problem is the serialized object look like this

<?xml version="1.0" encoding="UTF-8"?>
<SomeXml>
  <Zip2>
    <OriginalString>2094</OriginalString>
  </Zip2>
</SomeXml>

instead of this

<?xml version="1.0" encoding="UTF-8"?>
<SomeXml>
  <Zip2>2094</Zip2>
</SomeXml>

How can I serialize an object the way i want? Is this possible at all?

0

There are 0 answers