如何将交错的双精度数组转换为字符串并再次转换回来

本文关键字:转换 字符 字符串 回来 串并 数组 双精度 | 更新日期: 2023-09-27 18:36:40

我有一个交错的双精度数组,我需要将其转换为可以添加到xml文档中的内容,然后变回交错数组。有人知道一种方法可以做到这一点吗? 提前谢谢。

 public double[][] Coordinates { get; set; }
 //For context this is how I'm creating the array
 //convert the geography column back in lat'long points
 for (var g = 1; g <= geography.STNumGeometries(); g++)
 {
     var geo = geography.STGeometryN(g);
     var points = new List<double[]>();
     for (var i = 1; i <= geo.STNumPoints(); i++)
     {
         var point = new double[2];
         var sp = geography.STPointN(i);
         // we can safely round the lat/long to 5 decimal places 
         // as thats 1.11m at equator, reduces data transfered to client
         point[0] = Math.Round((double) sp.Lat, 5);
         point[1] = Math.Round((double) sp.Long, 5);
         points.Add(point);
     }
     transitLineSegment.Coordinates = points.ToArray();
 }

如何将交错的双精度数组转换为字符串并再次转换回来

linq 的强大功能(根据注释中的建议修改代码)

var doubleAR = new List<List<Double>>()
{
   new List<double>(){ 12.5, 12.6, 12.7 },
   new List<double>(){ .06 },
   new List<double>(){ 1.0, 2.0 }
};
   var asXml = new XElement("Data",
                            doubleAR.Select(sList => new XElement("Doubles", sList.Select (sl => new XElement("Double", sl) )))
                           );
    Console.WriteLine ( asXml );
/* Output
<Data>
  <Doubles>
    <Double>12.5</Double>
    <Double>12.6</Double>
    <Double>12.7</Double>
  </Doubles>
  <Doubles>
    <Double>0.06</Double>
  </Doubles>
  <Doubles>
    <Double>1</Double>
    <Double>2</Double>
  </Doubles>
</Data> */
// Then back
   List<List<Double>> asDoubleArray =
         XDocument.Parse( asXml.ToString() )
                  .Descendants("Doubles")
                  .Select (eDL => eDL.Descendants("Double")
                                     .Select (dl => (double) dl))
                                      .ToList())
                  .ToList();

您需要序列化对象以将其存储为字符串。 下面是一个示例: http://support.microsoft.com/kb/815813