David Smith

David Smith

  • NA
  • 2k
  • 0

Save XML Data Logic

Jul 30 2013 10:41 AM
Can someone help me better redesign logic below. I am saving data to XML file via serialization / deserialization. Review Logic Below. I want to simply this logic below.

private ColorObjectItems colorObject;

  private void WriteDataToFile()
  {
  try
  {
  int PartElement = 0;

  foreach (ColorPartNum cpn in colorPartNums)
  {
  m_file.PartNums.Add(new Color_PartNumber() { colorPartNumbers = cpn.PartNumber });

  List<Color_SerialNumner> colorSerialNumberList = new List<Color_SerialNumner>(cpn.SerialNumbers.Count());

  m_file.PartNums[PartElement].SerialNumbers = colorSerialNumberList;

  int serialElement = 0;

  foreach (ColorSerialNum csn in cpn.SerialNumbers)
  {
  colorSerialNumberList.Add(new Color_SerialNumber() { colorSerialNumber = csn.SerialNumber });

  List<Color_ColorObject> newColorObjectList = new List<Color_ColorObject>(csn.Colors.Count());
  colorSerialNumberList[serialElement].Colors = newColorObjectList;

  foreach (Color color in sn.Colors)
  {
  newColorList.Add(new newColorObjectList()
  {
  colorDatetime = DateTime.Now,
  isColorValidated = color.IsColorValidated
  });
  }

  ++serialElement;
  }

  ++PartElement;
  }

  XmlSerializer serializer = new XmlSerializer(typeof(ColorObjectItems));
  using (TextWriter textWriter = new StreamWriter(filename))
  {
  serializer.Serialize(textWriter, colorObject);
  textWriter.Flush();
  textWriter.Close();
  }
 
  }
  catch (Exception e)
  {
  MessageBox.Show("Error Occured: " + e.Message);
  }
  }



Answers (3)