I've wrote a .cs file in a predefined structure to read in a specific type of XML files. That file has a lot more classes underneath. Round about 67 at this time.
This is my code to read in the .tcx files which are XML files.
ModelData _xmlConfigData = new ModelData();
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(ModelData));
try
{
using (FileStream filestream = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))
{
_xmlConfigData = (ModelData)_xmlSerializer.Deserialize(filestream);
}
}
catch (Exception ex)
{
MessageBox.Show("Wrong XML structure read in. Please select another XML file:" + ex);
_xmlConfigData = null;
}
InvokeExtension.CreateListboxEntry(InvokeExtension.INFO + "TCX read successful", _lBox);
InvokeExtension.SetProgressbarValue(1000, _pBar);
There are no errors at reading the hundreds of thousands of lines. But, if I look at my class object (at debugging) the subclasses are listed alphabetically sorted. The only thing I do is to read the .tcx into a ModelData-class-object and write it out to a new file. This is my writer code:
After writing the new file the XML structure looks perfect, but some of the child elements are now sorted in other direction compared to the original file. This is causing problems. I don't know why and how to prevent that sorting of the child nodes. In my opinion it doens't matter in which sequence the child nodes are in an XML file.
By default, XmlSerializer will write out all the elements in alphabetical order.
You can change this by decorating all the elements with an [XmlElement]
attribute, specifying an index value for that attribute's XmlElementAttribute.Order
property.
Note that once you choose to do this for one element, you must do it for all elements.