Convert Spot to and from XML

I need to save my model into XML and later read it back.
I’m using the ‘MakeXElement’ and ‘LoadFromXElement’ from the samples.
All of my string attributes are saving fine but I’m stuck with how convert a ‘Spot’ into XML and read it back.

My Spot property:


        public Spot OBJECT_SPOT
        {
            get { return _oBJECT_SPOT; }
            set
            {
                if (_oBJECT_SPOT != value)
                {
                    Spot old = _oBJECT_SPOT;
                    _oBJECT_SPOT = value;
                    RaisePropertyChanged("OBJECT_SPOT", old, value);
                }
            }
        }
        private Spot _oBJECT_SPOT = Spot.None;

I’m trying to accomplish something like this (but it’s not working):


        public override XElement MakeXElement(XName n)
        {
            XElement e = base.MakeXElement(n);
            .....
            <b>e.Add(XHelper.Attribute("OBJECT_SPOT", this.OBJECT_SPOT, new Spot(0,0)));</b>
            .....

            return e;
        }


        public override void LoadFromXElement(XElement e)
        {
            base.LoadFromXElement(e);
            .....
            <b>this.OBJECT_SPOT = XHelper.Read("OBJECT_SPOT", e, "");</b>
            .....
        }

Any ideas?

As you have discovered, there are no XHelper static methods for reading or writing Spot values.

However, you could use Spot methods for converting to string (ToString) and from string (Spot.Parse).

So MakeXElement could do:
e.Add(XHelper.Attribute(“OBJECT_SPOT”, this.OBJECT_SPOT.ToString(), “0 0”);
and LoadFromXElement could do:
this.OBJECT_SPOT = Spot.Parse(XHelper.Read(“OBJECT_SPOT”, e, “0 0”));

I haven’t tried this, but something like this should work.
You might also need to deal with exceptions when calling Spot.Parse.
You’ll have to try that to see what happens.

That works great! Thanks Walter.