问题
I have a jagged array of doubles that I need to convert to something I can add to an xml document then turn back into the jagged array. Anyone know of a way to do this? Thanks in advance.
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();
}
回答1:
The power of linq (code modified per suggestions in comments)
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();
回答2:
You need to serialize the object to store it as a string. heres an example: http://support.microsoft.com/kb/815813
来源:https://stackoverflow.com/questions/10954142/how-to-convert-a-jagged-double-array-to-a-string-and-back-again