How to best work with serialization of TREX files in .NET core?

39 Views Asked by At

I'm looking at an XML Metadata Interchange (XMI) based document created by TreeAge. E.G. Here is the physical contents of a *.trex file:

<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI xmi:version="2.0" 
  xmlns:xmi="http://www.omg.org/XMI" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.2/notation" 
  xmlns:tree="http://www.treeage.com/modeldefs/tree">
  <tree:Tree xmi:id="_Uj1WYAR_EemNnJYeA_zjSg">
     <Node xmi:id="_nem4Bx6OEemrY_k4als23a" NameID="Node10" Label="Tenth_Node_Name" NodeType="TerminalNode">
     <Prob xmi:id="_nem4CB6OEemrY_k4bks23a" Value="Tenth_Node_Expression"/>
        <MarkovData xsi:type="tree:MarkovJumpTransitionData" xmi:id="_nem4CR6OEemrY_k4bks23a" MarkovJumpState="Jump">
          <Modification xmi:id="_nem4Ch6OEemrY_k4bks23a" Tracker="Tenth_Node_Tracker_1" Value="Tenth_Node_Tracker_1_Expression"/>
          <Modification xmi:id="_nem4Cx6OEemrY_k4bks23a" Tracker="Tenth_Node_Tracker_2" Value="Tenth_Node_Tracker_2_Expression"/>
          <Modification xmi:id="_nem4DB6OEemrY_k4bks23a" Tracker="Tenth_Node_Tracker_3" Value="Tenth_Node_Tracker_3_Expression"/>
         <Modification xmi:id="_nem4DR6OEemrY_k4cjs23a" Tracker="Tenth_Node_Tracker_4" Value="Tenth_Node_Tracker_4_Expression"/>
          </MarkovData>
     </Node>
      ..// Bunches of XML data
enter code here
  </tree:Tree>
</xmi:XMI>

UNIT TEST

I don't want hacks in the unit test. I wish TreeAgeXml was generated code, but I don't know how. How can I fix hacks? How can I generate the TreeAgeXml C# class?

        public void ShouldBuildATree_Test()
        {
            string filePath =  "Files/brianTest.trex";// Similar example above
            string fileXml = System.IO.File.ReadAllText(filePath);

            var treeAge = TrexUtil.BuildTreeFromXml(fileXml);
            var serializedXml = TrexUtil.SerialzeTreeAgeToXml(treeAge);

            serializedXml.ShouldBe(fileXml);
        }

        public static TreeAgeXml BuildTreeFromXml(string xml)
        {
            xml = xml.Replace("xsi:type", "xsitype"); // HACK
            xml = xml.Replace("xsi:nil", "xsinil"); // HACK
            var serializer = new XmlSerializer(typeof(XML.TreeAgeXml));
            XML.TreeAgeXml treeage = null;
            using (TextReader reader = new StringReader(xml))
            {
                treeage = (XML.TreeAgeXml)serializer.Deserialize(reader);
            }

            return treeage;
        }

        //TreeAgeXml is built out by hand over time through trial and error
        public static string SerialzeTreeAgeToXml(TreeAgeXml treeAge)
        {
            XmlSerializer xs = new XmlSerializer(typeof(TreeAgeXml));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("xmi", "http://www.omg.org/XMI");
            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("notation", "http://www.eclipse.org/gmf/runtime/1.0.2/notation");
            ns.Add("tree", "http://www.treeage.com/modeldefs/tree");

            string xml = "";
            using(StringWriter textWriter = new StringWriter())
            {
                xs.Serialize(textWriter, treeAge, ns);
                xml = textWriter.ToString();
            }

            // Hacks for things that didn't serialize quite right
            xml = xml.Replace("xsitype", "xsi:type");
            xml = xml.Replace("xsinil", "xsi:nil");
            xml = xml.Replace("&gt;", ">");
            xml = xml.Replace("\" />", "\"/>");
            xml = xml.Replace("\t", "&#x9;");

            // Hacks Manually insert the first two lines, because they never come out right
            var lines = xml.Split('\n');
            lines[0] = "<?xml version=\"1.0\" encoding=\"ASCII\"?>";
            lines[1] = "<xmi:XMI xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:notation=\"http://www.eclipse.org/gmf/runtime/1.0.2/notation\" xmlns:tree=\"http://www.treeage.com/modeldefs/tree\">";
            xml = String.Join('\n', lines);
            xml = xml + '\n';

            return xml;
        }
0

There are 0 best solutions below