I have an XML file (from somewhere) containing matrix values, which I wish to get into my code as double[][] objects. The XML contains table nodes, which look like standard serialized double[][] objects:
<table type="System.Double[][]"><table type="System.Double[]"><el type="System.Double">0.005</el><el type="System.Double">0.001</el><el type="System.Double">0.007</el><el type="System.Double">-0.012</el></table><table type="System.Double[]"><el type="System.Double">0.033</el><el type="System.Double">-0.146</el><el type="System.Double">-0.008</el><el type="System.Double">0.006</el></table><table type="System.Double[]"><el type="System.Double">-0.002</el><el type="System.Double">-0.004</el><el type="System.Double">-0.004</el><el type="System.Double">-0.003</el></table><table type="System.Double[]"><el type="System.Double">0</el><el type="System.Double">0</el><el type="System.Double">0</el><el type="System.Double">0</el></table></table>
Since not the whole XML is in this form, I only extract those nodes as XmlNode (since XElements don’t have InnerXml). Lets call this myMatrixXmlNode.
Then, I try to put that into a MemoryStream, and then deserialize from that:
var deserializer = new XmlSerializer(typeof(double[][]));
var myMatrix = (double[][])deserializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(myMatrixXmlNode.InnerXml)));
This throws me a <table xmlns=''> was not expected. error, for which I have not found a solution yet.. and I’m geting really annoyed by this.
You can not use standart Xml serializer for deserializing this Xml into
double[][].Format for
double[][]Xml serialization is like:You can try parse thos Xml manually using LinqToXml or transforming it to corresponding format.