C # XML Serializing a Double [] for a Single Space Element

I am writing an application in C # for serialization and a double or float array for a single XML element, which is a list of values ​​in an array, separated by spaces.

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

for an XML element:

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

I am trying to use XmlSerializer to do serialization. Any help on how to do this would simply be greatly appreciated.

Tim

+3
source share
1 answer

You can try something like the following. My example uses LINQ. If you are using VS2005 or earlier, let me know and I will 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(); }
  }
}
+4
source

Source: https://habr.com/ru/post/1708963/


All Articles