- Home /
Columns in XML serialization
I have a string[][] array that I serialize into an XML file, but it all gets plopped into a single column, despite the 2D nature of the array. The serializer seems to at least partially understand 2D arrays because the order the data is in the XML file has the data from the arrays one row at a time, though I might be misunderstanding how the array gets parsed
How should I go about changing the structure of my serialized XML file, or can this be achieved automatically using some sneaky part of the serializer that I didn't manage to find? Bonus points if it's possible to add little headers for each column or row
public static void SaveData_AsXML(string[][] _data)
{
XmlSerializer _serializer = new XmlSerializer(typeof(string[][]));
FileStream _stream = new FileStream(Path.Combine(dataPath, fileName), FileMode.Create);
_serializer.Serialize(_stream, _data);
}
Result (two items in the array, starting at lines 2 and 7):
Uhm, have you actually looked at the generated xml data? X$$anonymous$$L represents structured and named data. There is no real concept of an "array" in X$$anonymous$$L. X$$anonymous$$L consists of data nodes (elements) which can be structured any way you want. Single arrays generally get serialized into a root node and several nested nodes for the elements. However jagged arrays are usually serialized into a root node, several nested nodes (one for each outer array element) and each of those nested nodes get the actual elements (of the inner arrays) as nested elements. However There are no names available for the different elements needed.
Finally how do you actually convert your X$$anonymous$$L into a spread sheet? I guess the serialization to X$$anonymous$$L actually works but your importing into a spreadsheed does not. There's no generally valid way how X$$anonymous$$L data should be interpreted without a DTD or XSD.
Your data is just a chunk of strings. So adding any sort of headers or na$$anonymous$$g is pretty impossible without creating proper data classes which represents your data in a structured way.
It's still not clear what you're trying to achieve here. Where does the data come from and what should it look like in the end? Why serialize to X$$anonymous$$L if you see$$anonymous$$gly want the data in a spreadsheet? Wouldn't "CSV" make more sense?