views:

787

answers:

1

I am writing an app in C# to serialize and array of double or float to a single XML element that is a space-delimited list of the values in the array.

double[] d = new double[4] { 1.0, 2.0, 3.0, 4.0 };

to the XML element:

<ArrayOfDouble type="double">1.0 2.0 3.0 4.0</ArrayOfDouble>

I am trying to use the XmlSerializer to perform the serialization. Any help on how to get this done simply would be greatly appreciated.

Tim

+2  A: 

You can try something like the following. My sample uses LINQ. If you are using VS2005 or earlier let me know and I'll update the answer.

class Example {
  [XmlIgnore]
  public double[] DoubleValue { get ... set ... }

  public string ArrayOfDouble {
    get { return DoubleValue.Select(x => x.ToString()).Aggregate( (x,y) => x + " " y); }
    set { Doublevalue = value.Split(" ").Select(x => Double.Parse(x)).ToArray(); }
  }
}
JaredPar
Very elegant solution +1
Jose Basilio
I am actually developing under SharpDevelop v2.2.1Thanks for your suggestion.
Note that when writing serialization code you should be *very* careful about the culture - usually specifying "invariant". I'd also be a little careful about looped string concatenation (preferring StringBuilder); sometimes a few extra lines is very much worth it.
Marc Gravell