• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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
0
rondotorugantiatgmaildotcom
Asked:
rondotorugantiatgmaildotcom
  • 2
1 Solution
 
rondotorugantiatgmaildotcomAuthor Commented:
THIS IS URGENT. Greatly appreciate all help.
0
 
Miguel OzSoftware EngineerCommented:
Use the  SetLength method to reset the fs starting point after reading.
Replace:
serializer.Serialize(fs, objSimulatorConfig);
with:
fs.SetLength(0);
serializer.Serialize(fs, objSimulatorConfig);

Open in new window

Note: Without this reset, your program is appending the xml in memory to the file.
0
 
rondotorugantiatgmaildotcomAuthor Commented:
That worked great..thank you! And I see what I was doing wrong..its like duh! in fact.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now