简体   繁体   中英

C# XML Serialization of double[] to single space-delimited element

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

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(); }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM