Index: src/sans/perspectives/fitting/pagestate.py
===================================================================
--- src/sans/perspectives/fitting/pagestate.py	(revision eddb6eca9b813af3dd1f8c272f767fec8e5252fa)
+++ src/sans/perspectives/fitting/pagestate.py	(revision ac5b69d317038c3025e6c4a3397e52b2a3012a79)
@@ -20,4 +20,5 @@
 
 import xml.dom.minidom
+from xml.dom.minidom import parseString
 from lxml import etree
 
@@ -672,10 +673,21 @@
             # We are appending to an existing document
             newdoc = doc
-            top_element = newdoc.createElement(FITTING_NODE_NAME)
+            try:
+                top_element = newdoc.createElement(FITTING_NODE_NAME)
+            except:
+                string = etree.tostring(doc, pretty_print=True)
+                newdoc = parseString(string)
+                top_element = newdoc.createElement(FITTING_NODE_NAME)
             if entry_node is None:
                 newdoc.documentElement.appendChild(top_element)
             else:
-                entry_node.appendChild(top_element)
-            
+                try:
+                    entry_node.appendChild(top_element)
+                except:
+                    node_name = entry_node.tag
+                    node_list = newdoc.getElementsByTagName(node_name)
+                    entry_node = node_list.item(0)
+                    entry_node.appendChild(top_element)
+                    
         attr = newdoc.createAttribute("version")
         attr.nodeValue = '1.0'
@@ -750,5 +762,5 @@
         for item in list_of_state_parameters:
             element = newdoc.createElement(item[0])
-            com = "self._toXML_helper(list=self.%s,"
+            com = "self._toXML_helper(thelist=self.%s,"
             com += " element=element, newdoc=newdoc)"
             exec com % item[1]
@@ -762,5 +774,5 @@
             return None
         else:
-            return newdoc.toprettyxml()
+            return newdoc
         
     def _fromXML_helper(self, node, list):
@@ -1252,4 +1264,5 @@
                 state = PageState()
                 state.fromXML(node=nodes[0])
+                
         except:
             logging.info("XML document does not contain fitting information.\n %s" % sys.exc_value)
@@ -1257,5 +1270,5 @@
         return state
       
-    def _parse_entry(self, dom):
+    def _parse_save_state_entry(self, dom):
         """
         Parse a SASentry
@@ -1268,5 +1281,22 @@
         node = dom.xpath('ns:data_class', namespaces={'ns': CANSAS_NS})
         if not node or node[0].text.lstrip().rstrip() != "Data2D":
-            return CansasReader._parse_entry(self, dom)
+            return_value, _ = self._parse_entry(dom)
+            numpy.trim_zeros(return_value.x)
+            numpy.trim_zeros(return_value.y)
+            numpy.trim_zeros(return_value.dy)
+            size_dx = return_value.dx.size
+            size_dxl = return_value.dxl.size
+            size_dxw = return_value.dxw.size
+            if size_dxl == 0 and size_dxw == 0:
+                return_value.dxl = None
+                return_value.dxw = None
+                numpy.trim_zeros(return_value.dx)
+            elif size_dx == 0:
+                return_value.dx = None
+                size_dx = size_dxl
+                numpy.trim_zeros(return_value.dxl)
+                numpy.trim_zeros(return_value.dxw)
+                            
+            return return_value, _
         
         #Parse 2D
@@ -1539,5 +1569,5 @@
                     for entry in entry_list:
                         try:
-                            sas_entry = self._parse_entry(entry)
+                            sas_entry, _ = self._parse_save_state_entry(entry)
                         except:
                             raise
@@ -1608,13 +1638,14 @@
         # Sanity check
         if self.cansas == True:
-            
             # Add fitting information to the XML document
             doc = self.write_toXML(datainfo, fitstate)
             # Write the XML document
-            fd = open(filename, 'w')
-            fd.write(doc.toprettyxml())
-            fd.close()
         else:
-            fitstate.toXML(file=filename)
+            doc = fitstate.toXML(file=filename)
+        
+        # Save the document no matter the type
+        fd = open(filename, 'w')
+        fd.write(doc.toprettyxml())
+        fd.close()
         
     def write_toXML(self, datainfo=None, state=None):
@@ -1638,5 +1669,5 @@
                 state.data.run = [str(state.data.name)]
                 state.data.run_name[0] = state.data.name
-   
+            
             if issubclass(state.data.__class__,
                           sans.dataloader.data_info.Data1D):
@@ -1648,6 +1679,6 @@
             
         if state is not None:
-            state.toXML(doc=doc, file=data.filename, entry_node=sasentry)
-            
+            doc = state.toXML(doc=doc, file=data.filename, entry_node=sasentry)
+        
         return doc
     
