问题遇到的现象和发生背景
C#List动态嵌套另一个List对象,添加后,子List改变,父级list也发生改变,如何解决?
用代码块功能插入代码,请勿粘贴截图
class Com_Read_Write
{
public static void ReadData(string path)
{
CProgram.m_ChildModelData.Track.Clear();//读之前先清空list
CProgram.m_ChildModelData.Mark.Clear();
CProgram.ChildModelData TemporaryModelData ;//= new CProgram.ChildModelData();
XmlDocument xml = new XmlDocument();
//读取指定路径的XML文件
if (System.IO.File.Exists(path))//如果此文件存在
{
xml.Load(path);
}
else
{
MessageBox.Show("位置文件不存在");
}
//循环XML文件的节点(只能读取同级节点)
foreach (XmlNode item in xml.ChildNodes)//两个: item = 1开头声明,2 root
{
TemporaryModelData = new CProgram.ChildModelData();
foreach (XmlNode ite in item.ChildNodes)//root有ChildNodes, ite= ModelName
{
foreach (XmlNode itf in ite.ChildNodes)//itf = A_ChildModel
{
if (itf.Name == "A_ChildModel" || itf.Name == "B_ChildModel")
{
foreach (XmlNode it in itf.ChildNodes)//it="ChildName","A_ChildCount"
{
if (it.Name == "ChildName")
{
XmlElement element = (XmlElement)it;
CProgram.m_ChildModelData.ChildName = element.GetAttribute("ID");
TemporaryModelData.ChildName = element.GetAttribute("ID");
}
else if (it.Name == "A_ChildCount")
{
CProgram.A_ChildCount = Convert.ToInt32(it.InnerText);
}
foreach (XmlNode i in it.ChildNodes)
{
if (i.Name == "UseFlg")
{
CProgram.m_ChildModelData.UseFlg = Convert.ToBoolean(i.InnerText);
TemporaryModelData.UseFlg = Convert.ToBoolean(i.InnerText);
}
if (i.Name == "TrackCount")
{
CProgram.m_ChildModelData.TrackCount = Convert.ToInt32(i.InnerText);
TemporaryModelData.TrackCount = Convert.ToInt32(i.InnerText);
}
else if (i.Name == "Track_")
{
foreach (XmlNode itemss in i.ChildNodes)//----->UseFlg,TrackType,ParaType...
{
//将指定节点的值放入指定变量中
switch (itemss.Name)
{
case "UseFlg": CProgram.m_TrackDataTemp.UseFlg = Convert.ToBoolean(itemss.InnerText);
break;
case "TrackType": CProgram.m_TrackDataTemp.TrackType = Convert.ToInt32(itemss.InnerText);
break;
case "ParaType": CProgram.m_TrackDataTemp.ParaType = Convert.ToInt32(itemss.InnerText);
break;
case "Start_X": CProgram.m_TrackDataTemp.Start_X = Convert.ToDouble(itemss.InnerText);
break;
case "Start_Y": CProgram.m_TrackDataTemp.Start_Y = Convert.ToDouble(itemss.InnerText);
break;
case "Start_Z": CProgram.m_TrackDataTemp.Start_Z = Convert.ToDouble(itemss.InnerText);
break;
case "Center_X": CProgram.m_TrackDataTemp.Center_X = Convert.ToDouble(itemss.InnerText);
break;
case "Center_Y": CProgram.m_TrackDataTemp.Center_Y = Convert.ToDouble(itemss.InnerText);
break;
case "Center_Z": CProgram.m_TrackDataTemp.Center_Z = Convert.ToDouble(itemss.InnerText);
break;
case "Stop_X": CProgram.m_TrackDataTemp.Stop_X = Convert.ToDouble(itemss.InnerText);
break;
case "Stop_Y": CProgram.m_TrackDataTemp.Stop_Y = Convert.ToDouble(itemss.InnerText);
break;
case "Stop_Z": CProgram.m_TrackDataTemp.Stop_Z = Convert.ToDouble(itemss.InnerText);
break;
case "OpenValue": CProgram.m_TrackDataTemp.OpenValue = Convert.ToBoolean(itemss.InnerText);
break;
case "ValueNum": CProgram.m_TrackDataTemp.ValueNum = Convert.ToInt32(itemss.InnerText);
break;
case "Delay": CProgram.m_TrackDataTemp.Delay = Convert.ToInt32(itemss.InnerText);
break;
case "Weight": CProgram.m_TrackDataTemp.Weight = Convert.ToDouble(itemss.InnerText);
break;
case "SprayCount": CProgram.m_TrackDataTemp.SprayCount = Convert.ToInt32(itemss.InnerText);
break;
case "Space": CProgram.m_TrackDataTemp.Space = Convert.ToDouble(itemss.InnerText);
break;
}
}
CProgram.m_ChildModelData.Track.Add(CProgram.m_TrackDataTemp); //public List<TrackData> Track { get; set; }
TemporaryModelData.Track = CProgram.m_ChildModelData.Track.GetRange(0, CProgram.m_ChildModelData.Track.Count);
}
else if (i.Name == "Mark")
{
foreach (XmlNode itemss in i.ChildNodes)
{
switch (itemss.Name)
{
case "Type": CProgram.m_MarkDataTemp.Type = Convert.ToInt32(itemss.InnerText);
break;
case "FindCircleFlg": CProgram.m_MarkDataTemp.FindCircleFlg = Convert.ToInt32(itemss.InnerText);
break;
case "CCD_X": CProgram.m_MarkDataTemp.CCD_X = Convert.ToDouble(itemss.InnerText);
break;
case "CCD_Y": CProgram.m_MarkDataTemp.CCD_Y = Convert.ToDouble(itemss.InnerText);
break;
case "Pixel_X": CProgram.m_MarkDataTemp.Pixel_X = Convert.ToDouble(itemss.InnerText);
break;
case "Pixel_Y": CProgram.m_MarkDataTemp.Pixel_Y = Convert.ToDouble(itemss.InnerText);
break;
case "Angle": CProgram.m_MarkDataTemp.Angle = Convert.ToDouble(itemss.InnerText);
break;
case "Score": CProgram.m_MarkDataTemp.Score = Convert.ToDouble(itemss.InnerText);
break;
case "Diameter": CProgram.m_MarkDataTemp.Diameter = Convert.ToDouble(itemss.InnerText);
break;
case "MinLight": CProgram.m_MarkDataTemp.MinLight = Convert.ToInt32(itemss.InnerText);
break;
case "MaxLight": CProgram.m_MarkDataTemp.MaxLight = Convert.ToInt32(itemss.InnerText);
break;
case "LED1": CProgram.m_MarkDataTemp.LED1 = Convert.ToInt32(itemss.InnerText);
break;
case "LED2": CProgram.m_MarkDataTemp.LED2 = Convert.ToInt32(itemss.InnerText);
break;
case "Row1": CProgram.m_MarkDataTemp.Row1 = Convert.ToDouble(itemss.InnerText);
break;
case "Col1": CProgram.m_MarkDataTemp.Col1 = Convert.ToDouble(itemss.InnerText);
break;
case "Row2": CProgram.m_MarkDataTemp.Row2 = Convert.ToDouble(itemss.InnerText);
break;
case "Col2": CProgram.m_MarkDataTemp.Col2 = Convert.ToDouble(itemss.InnerText);
break;
}
}
CProgram.m_ChildModelData.Mark.Add(CProgram.m_MarkDataTemp);
TemporaryModelData.Mark = CProgram.m_ChildModelData.Mark.GetRange(0, CProgram.m_ChildModelData.Mark.Count);
}
}
}
}
if (itf.Name == "A_ChildModel")
{
CProgram.A_GlueData.Add(TemporaryModelData);
}
else if (itf.Name == "B_ChildModel")
{
CProgram.B_GlueData.Add(TemporaryModelData);
}
}
}
}
}