BCS
BCS

Reputation: 78496

XML and DataContractSerializer

I have classes like this

[DataContract(Namespace = "")]
public class Foo
{
    [DataMember(Order = 0)]
    Bar bar;
}

[DataContract(Namespace = "")]
public class Bar
{
    Baz baz;

    [DataMember(Order = 0)]
    string TheBaz
    {
        get { baz.ToString(); }
        set { SomeOtherCode(value); }
    }
}

I want this to generate XML like this

<Foo>
    <Bar>String from baz.ToString()</Bar>
</Foo>

but am getting something more like:

<Foo>
    <Bar><TheBaz>String from baz.ToString()</TheBaz></Bar>
</Foo>

is it possible to fix this? This artical says that one of the disadvantages of DataContractSerializer is:

  1. No control over how the object is serialized outside of setting the name and the order

leading me to wonder is this is not a solvable problem.


I known this can be done with IXmlSerializable and ReadXml/WriteXml because I'm supposed to be removing code that does just that.

Upvotes: 1

Views: 2024

Answers (2)

casperOne
casperOne

Reputation: 74530

Implement IXmlSerializable on the Bar class, and then have it output <Bar>String from baz.ToString()</Bar> when serialized.

You can leave the Foo class as is, and the DataContractSerializer will take care of the rest.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062492

I realized my first answer was completely bogus - but you can cheat with properties:

[DataContract(Namespace = "")]
public class Foo
{
    [DataMember(Order = 0, Name="Bar")]
    private string BazString {
        get {
            return bar == null ? null : bar.TheBaz.ToString();
        }
        set {
            if(value == null) {
                bar = null;
            }
            else {
                if(bar == null) bar = new Bar();
                bar.TheBaz = value;
            }
        }
    }

    Bar bar;
}

Upvotes: 1

Related Questions