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.lstFile Manage = new List<string>();
System.IO.FileStream fs = new FileStream(System.IO.Direc tory.GetCu rrentDirec tory() + "\\ManagerFile.xml", FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(Simul atorConfig ));
//Check for 51 file limit
XmlReader rdr = XmlReader.Create(fs);
objSimulatorConfig = (SimulatorConfig)serialize r.Deserial ize(rdr);
if (objSimulatorConfig.lstFil eManage.Co unt() > objSimulatorConfig.iMaxFil es)
{
for (int i = objSimulatorConfig.iMaxFil es - 4; i <= objSimulatorConfig.iMaxFil es - 1; i++)
{
System.IO.File.Delete(objS imulatorCo nfig.lstFi leManage.E lementAt(i ).ToString ());
}
}
objSimulatorConfig.lstFile Manage.Add (destDirec tory + "\\" + imageFileName); ;
serializer.Serialize(fs, objSimulatorConfig);
fs.Close();
}
catch (Exception ex)
{
ex = new Exception("",ex.InnerExcep tion);
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.ti f</string>
<string>C:\Image\C6_0_1.ti f</string>
<string>C:\Image\C6_0_2.ti f</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.ti f</string>
</lstFileManage>
</SimulatorConfig>
ManagerFile.xml
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.lstFile
System.IO.FileStream fs = new FileStream(System.IO.Direc
XmlSerializer serializer = new XmlSerializer(typeof(Simul
//Check for 51 file limit
XmlReader rdr = XmlReader.Create(fs);
objSimulatorConfig = (SimulatorConfig)serialize
if (objSimulatorConfig.lstFil
{
for (int i = objSimulatorConfig.iMaxFil
{
System.IO.File.Delete(objS
}
}
objSimulatorConfig.lstFile
serializer.Serialize(fs, objSimulatorConfig);
fs.Close();
}
catch (Exception ex)
{
ex = new Exception("",ex.InnerExcep
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.ti
<string>C:\Image\C6_0_1.ti
<string>C:\Image\C6_0_2.ti
</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.ti
</lstFileManage>
</SimulatorConfig>
ManagerFile.xml
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked great..thank you! And I see what I was doing wrong..its like duh! in fact.
ASKER