0

我有一个参差不齐的双精度数组,我需要将其转换为可以添加到 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();
 }
4

2 回答 2

3

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();
于 2012-06-08T18:52:04.620 回答
0

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

于 2012-06-08T18:40:51.400 回答