Link to home
Start Free TrialLog in
Avatar of rondotorugantiatgmaildotcom
rondotorugantiatgmaildotcom

asked on

Trouble with XML

Hi

I'm having trouble with some serialization to XML.

I'm trying to a serializable class to an xml file but it gets appended with a whole new xml root and contents everytime and also isn't working well when i try to read from it. Here's code and the XML file, can someone fix this? Thanks

private void ManageFiles(string destDirectory, string imageFileName)
        {
            try
            {
                InitializeFile();
                SimulatorConfig objSimulatorConfig = new SimulatorConfig();
                objSimulatorConfig.lstFileManage = new List<string>();
               
                System.IO.FileStream fs = new FileStream(System.IO.Directory.GetCurrentDirectory() + "\\ManagerFile.xml", FileMode.OpenOrCreate);
                XmlSerializer serializer = new XmlSerializer(typeof(SimulatorConfig));
               
                //Check for 51 file limit
                XmlReader rdr = XmlReader.Create(fs);
               
                objSimulatorConfig = (SimulatorConfig)serializer.Deserialize(rdr);

                if (objSimulatorConfig.lstFileManage.Count() > objSimulatorConfig.iMaxFiles)
                {
                    for (int i = objSimulatorConfig.iMaxFiles - 4; i <= objSimulatorConfig.iMaxFiles - 1; i++)
                    {
                        System.IO.File.Delete(objSimulatorConfig.lstFileManage.ElementAt(i).ToString());
                    }
                }

                objSimulatorConfig.lstFileManage.Add(destDirectory + "\\" + imageFileName); ;
                serializer.Serialize(fs, objSimulatorConfig);
                fs.Close();
            }
            catch (Exception ex)
            {
                ex = new Exception("",ex.InnerException);
                throw ex;
            }
        }

INCORRECT HANDLING OF NODE
----------------------------------------------------

<?xml version="1.0"?>
<SimulatorConfig xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <iMaxFiles>51</iMaxFiles>
  <lstFileManage>
    <string>C:\Image\C6_0_0.tif</string>
    <string>C:\Image\C6_0_1.tif</string>
    <string>C:\Image\C6_0_2.tif</string>
</SimulatorConfig><?xml version="1.0"?>
<SimulatorConfig xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <iMaxFiles>51</iMaxFiles>
  <lstFileManage>
    <string>C:\Image\C7_0_0.tif</string>
  </lstFileManage>
</SimulatorConfig>
ManagerFile.xml
Avatar of rondotorugantiatgmaildotcom
rondotorugantiatgmaildotcom

ASKER

THIS IS URGENT. Greatly appreciate all help.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked great..thank you! And I see what I was doing wrong..its like duh! in fact.