更新时间:2022-06-25 20:13:39
可以试试下面的代码吗?
Could you please try below code?
public void btn_Submit_Click(object sender, EventArgs e)
{
List<KeyValuePair<string, Tuple<int, int>>> d = new List<KeyValuePair<string, Tuple<int, int>>>
{
new KeyValuePair<string, Tuple<int, int>>("1",Tuple.Create<int,int>(1,1)),
new KeyValuePair<string, Tuple<int, int>>("2",Tuple.Create<int,int>(2,2)),
new KeyValuePair<string, Tuple<int, int>>("3",Tuple.Create<int,int>(3,3)),
new KeyValuePair<string, Tuple<int, int>>("4",Tuple.Create<int,int>(4,4)),
new KeyValuePair<string, Tuple<int, int>>("5",Tuple.Create<int,int>(5,5))
};
List<Coordinate> cv = new List<Coordinate>();
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
}
SaveValues(cv);
}
public void SaveValues(List<Coordinate> v)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Coordinate>), new XmlRootAttribute("Coordinates"));
using (TextWriter textWriter = new StreamWriter(@"F:\Vista\Exporting into XML\Test1\Coordinates output.xml", true))
{
serializer.Serialize(textWriter, v);
}
MessageBox.Show("Coordinates were exported successfully", "Message");//Let the user know the export was succesfull
}
更新代码以包含字典...
Updated code to have dictionary...
public void btn_Submit_Click(object sender, EventArgs e)
{
Dictionary<string, Tuple<int, int>> d = new Dictionary<string, Tuple<int, int>>();
int X_Co = 1;
int Y_Co = 1;
var t = new Tuple<int, int>(X_Co, Y_Co);
d.Add("1", t);
X_Co = 2;
Y_Co = 2;
t = new Tuple<int, int>(X_Co, Y_Co);
d.Add("2", t);
List<Coordinate> cv = new List<Coordinate>();
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
}
SaveValues(cv);
}
更新了代码,每个循环都有单独的文件.
Updated code to have separate files for every loop.
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
List<Coordinate> cv = new List<Coordinate>();
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
SaveValues(cv);
}