例程终结者 2022-11-08 07:59 采纳率: 88%
浏览 16
已结题

C#List动态嵌套另一个List对象,添加后,子List改变,父级list也发生改变,如何解决?

问题遇到的现象和发生背景

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);
                        }
                    }
                }
            }


           
        }

  • 写回答

2条回答 默认 最新

  • 於黾 2022-11-08 09:16
    关注

    你这是3重循环,你还是把new放到了外层循环去了,你的add在内层循环啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗