Index: src/sans/dataloader/readers/cansas_reader.py
===================================================================
--- src/sans/dataloader/readers/cansas_reader.py	(revision b65ae9053d3c25a22a6e885dc556e9ca39da5014)
+++ src/sans/dataloader/readers/cansas_reader.py	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -28,4 +28,5 @@
 
 _ZERO = 1e-16
+PREPROCESS = "xmlpreprocess"
 HAS_CONVERTER = True
 try:
@@ -212,4 +213,11 @@
                             data1d.filename = name
                             data1d.meta_data["loader"] = "CanSAS 1D"
+                            
+                            # Get all preprocessing events
+                            self.reader.setProcessingInstructions()
+                            data1d.meta_data[PREPROCESS] = \
+                                    self.reader.processingInstructions
+                            
+                            # Parse the XML file
                             return_value, extras = \
                                 self._parse_entry(entry, ns, data1d)
@@ -247,12 +255,12 @@
         return None
     
-    def _create_unique_key(self, dictionary, name, i):
+    def _create_unique_key(self, dictionary, name, i = 0):
         """
         Create a unique key value for any dictionary to prevent overwriting
-        
+        Recurses until a unique key value is found.
         
         :param dictionary: A dictionary with any number of entries
         :param name: The index of the item to be added to dictionary
-        :param i: The number to be appended to the name
+        :param i: The number to be appended to the name, starts at 0
         """
         if dictionary.get(name) is not None:
@@ -291,5 +299,5 @@
                     if HAS_CONVERTER == True:
                         try:
-                            ## Check local units - bad units should raise KeyError
+                            ## Check local units - bad units raise KeyError
                             Converter(local_unit)
                             data_conv_q = Converter(attr['unit'])
@@ -347,4 +355,6 @@
                             CANSAS_NS.get(self.cansas_version).get("ns"), "}")
         unit = ''
+        tagname = ''
+        tagname_original = ''
         
         # Go through each child in the parent element
@@ -440,8 +450,10 @@
                         exec item
                 # Don't bother saving empty information unless it is a float
-                if cs_values.ns_variable is not None and node_value is not None and \
+                if cs_values.ns_variable is not None and \
+                            node_value is not None and \
                             node_value.isspace() == False:
                     # Format a string and then execute it.
-                    store_me = cs_values.ns_variable.format("data1d", node_value, tagname)
+                    store_me = cs_values.ns_variable.format("data1d", \
+                                                            node_value, tagname)
                     exec store_me
                 # Get attributes and process them
@@ -503,9 +515,6 @@
         
         main_node = doc.createElement("SASroot")
-        if self.cansas_version == "1.1":
-            pi = doc.createProcessingInstruction('xml-stylesheet', \
-                                    'type="text/xsl" href="cansasxml-html.xsl"')
-            root = doc.firstChild
-            doc.insertBefore(pi, root)
+        
+        doc = self.setProcessingInstructions(doc, datainfo.meta_data[PREPROCESS])
         main_node.setAttribute("version", self.cansas_version)
         main_node.setAttribute("xmlns", ns)
@@ -789,2 +798,13 @@
         fd.write(doc.toprettyxml())
         fd.close()
+    
+    ## Once I convert the writer to lxml from minidom
+    ## This can be moved into xml_reader
+    def setProcessingInstructions(self, minidomObject, dic):
+        xmlroot = minidomObject.firstChild
+        for item in dic:
+            pi = minidomObject.createProcessingInstruction(item, dic[item])
+            minidomObject.insertBefore(pi, xmlroot)
+        return minidomObject
+    
+    
Index: src/sans/dataloader/readers/xml_reader.py
===================================================================
--- src/sans/dataloader/readers/xml_reader.py	(revision 76cd1aeb8047424a5dc9e1d0e5c3eaa0f17c3bb4)
+++ src/sans/dataloader/readers/xml_reader.py	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -14,5 +14,5 @@
 
 from lxml import etree
-parser = etree.ETCompatXMLParser()
+parser = etree.ETCompatXMLParser(remove_comments=True, remove_pis=False)
 
 class XMLreader():
@@ -23,8 +23,10 @@
     schema = None
     schemadoc = None
+    processingInstructions = None
     
     def __init__(self, xml = None, schema = None, root = None):
         self.xml = xml
         self.schema = schema
+        self.processingInstructions = {}
         if xml is not None:
             self.setXMLFile(xml, root)
@@ -48,4 +50,7 @@
     
     def setXMLFile(self, xml, root = None):
+        """
+        Set the XML file and parse
+        """
         try:
             self.xml = xml
@@ -58,4 +63,7 @@
     
     def setSchema(self, schema):
+        """
+        Set the schema file and parse
+        """
         try:
             self.schema = schema
@@ -78,5 +86,5 @@
     def findInvalidXML(self):
         """
-        Finds the first offending element that should not be present in an XML file
+        Finds the first offending element that should not be present in XML file
         """
         firstError = ""
@@ -96,2 +104,45 @@
         self.setSchema(self.schema)
         
+    def toString(self, etreeElement):
+        """
+        Converts and etree element into a string
+        """
+        return etree.tostring(etreeElement)
+    
+    def setProcessingInstructions(self):
+        """
+        Take out all processing instructions and create a dictionary from them
+        """
+        dic = {}
+        pi = self.xmlroot.getprevious()
+        while pi is not None:
+            attr = {}
+            pi_name = ""
+            pi_string = self.toString(pi)
+            if isinstance(pi_string, str):
+                pi_string = pi_string.replace("<?", "").replace("?>", "")
+                split = pi_string.split(" ", 1)
+                pi_name = split[0]
+                attr = split[1]
+            new_pi_name = self._create_unique_key(dic, pi_name)
+            dic[new_pi_name] = attr
+            pi = pi.getprevious()
+        self.processingInstructions = dic
+        
+    def _create_unique_key(self, dictionary, name, i = 0):
+        """
+        Create a unique key value for any dictionary to prevent overwriting
+        Recurses until a unique key value is found.
+        
+        :param dictionary: A dictionary with any number of entries
+        :param name: The index of the item to be added to dictionary
+        :param i: The number to be appended to the name, starts at 0
+        """
+        if dictionary.get(name) is not None:
+            i += 1
+            name = name.split("_")[0]
+            name += "_{0}".format(i)
+            name = self._create_unique_key(dictionary, name, i)
+        return name
+    
+        
Index: test/sansdataloader/test/isis_1_0_write_test.xml
===================================================================
--- test/sansdataloader/test/isis_1_0_write_test.xml	(revision c6ca23d11f042afdc56f0237d73b2f665b24af49)
+++ test/sansdataloader/test/isis_1_0_write_test.xml	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -1,3 +1,4 @@
 <?xml version="1.0" ?>
+<?xml-stylesheet type="text/xsl" href="cansasxml-html.xsl" ?>
 <SASroot version="1.0" xmlns="cansas1d/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="cansas1d/1.0 http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd">
 	<SASentry>
Index: test/sansdataloader/test/isis_1_1_write_test.xml
===================================================================
--- test/sansdataloader/test/isis_1_1_write_test.xml	(revision c6ca23d11f042afdc56f0237d73b2f665b24af49)
+++ test/sansdataloader/test/isis_1_1_write_test.xml	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -1,4 +1,4 @@
 <?xml version="1.0" ?>
-<?xml-stylesheet type="text/xsl" href="cansasxml-html.xsl"?>
+<?xml-stylesheet type="text/xsl" href="cansas1d.xsl" ?>
 <SASroot version="1.1" xmlns="urn:cansas1d:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:cansas1d:1.1 http://www.cansas.org/formats/1.1/cansas1d.xsd">
 	<SASentry>
Index: test/sansdataloader/test/utest_cansas.py
===================================================================
--- test/sansdataloader/test/utest_cansas.py	(revision c6ca23d11f042afdc56f0237d73b2f665b24af49)
+++ test/sansdataloader/test/utest_cansas.py	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -20,5 +20,5 @@
 
 from lxml import etree
-import xml.dom.minidom
+from xml.dom import minidom
  
 CANSAS_FORMAT = cansasConstants.CANSAS_FORMAT
@@ -50,5 +50,5 @@
             name = self.get_number_of_entries(dictionary, name, i)
         return name
-    
+       
 
     def test_xml_validate(self):
@@ -82,5 +82,5 @@
         self.assertTrue(data.title == "TK49 c10_SANS")
         self.assertTrue(data.x.size == 138)
-        self.assertTrue(len(data.meta_data) == 2)
+        self.assertTrue(len(data.meta_data) == 3)
         self.assertTrue(data.detector[0].distance_unit == "mm")
         self.assertTrue(data.detector[1].distance_unit == "mm")
@@ -90,4 +90,5 @@
         self.assertTrue(data.detector[1].distance == 4145.02)
         self.assertTrue(data.process[0].name == "Mantid generated CanSAS1D XML")
+        self.assertTrue(data.meta_data["xmlpreprocess"]["xml-stylesheet"] != None)
         
     
@@ -155,8 +156,10 @@
         self.assertFalse(reader7.validateXML())
         
-    
+       
     def test_old_cansas_files(self):
         reader1 = XMLreader(self.cansas1d, self.schema_1_0)
         self.assertTrue(reader1.validateXML())
+        file_loader = Loader()
+        file1 = file_loader.load(self.cansas1d)
         reader2 = XMLreader(self.cansas1d_units, self.schema_1_0)
         self.assertTrue(reader2.validateXML())
@@ -166,5 +169,4 @@
         self.assertTrue(reader4.validateXML())
         
-    
     def test_save_cansas_v1_0(self):
         filename = "isis_1_0_write_test.xml"
@@ -186,5 +188,46 @@
             self._check_data(written_data)
         
-
+        
+    def test_processing_instructions(self):
+        reader = XMLreader(self.isis_1_1, self.schema_1_1)
+        valid = reader.validateXML()
+        if valid:
+            ## find the processing instructions and make into a dictionary
+            dic = self.getProcessingInstructions(reader)
+            self.assertTrue(dic == {'xml-stylesheet': 'type="text/xsl" href="cansas1d.xsl" '})
+            
+            xml = "<test><a><b><c></c></b></a></test>"
+            xmldoc = minidom.parseString(xml)
+            
+            ## take the processing instructions and put them back in
+            xmldoc = self.setProcessingInstructions(xmldoc, dic)
+            xml_output = xmldoc.toprettyxml()
+            
+    
+    def setProcessingInstructions(self, minidomObject, dic):
+        xmlroot = minidomObject.firstChild
+        for item in dic:
+            pi = minidomObject.createProcessingInstruction(item, dic[item])
+            minidomObject.insertBefore(pi, xmlroot)
+        return minidomObject
+    
+    
+    def getProcessingInstructions(self, XMLreaderObject):
+        dict = {}
+        pi = XMLreaderObject.xmlroot.getprevious()
+        i = 0
+        while pi is not None:
+            attr = {}
+            pi_name = ""
+            pi_string = etree.tostring(pi)
+            if isinstance(pi_string, str):
+                pi_string = pi_string.replace("<?", "").replace("?>", "")
+                split = pi_string.split(" ", 1)
+                pi_name = split[0]
+                attr = split[1]
+            dict[pi_name] = attr
+            pi = pi.getprevious()
+        return dict
+        
 if __name__ == '__main__':
     unittest.main()    
Index: test/sansdataloader/test/write_test.xml
===================================================================
--- test/sansdataloader/test/write_test.xml	(revision 75eeb4259b6c50a745eb6eec9691323e3c4f077f)
+++ test/sansdataloader/test/write_test.xml	(revision ea675417e1492f1711d7dea97a78874a660ee448)
@@ -1,3 +1,4 @@
 <?xml version="1.0" ?>
+<?xml-stylesheet type="text/xsl" href="example.xsl" ?>
 <SASroot version="1.0" xmlns="cansas1d/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="cansas1d/1.0 http://svn.smallangles.net/svn/canSAS/1dwg/trunk/cansas1d.xsd">
 	<SASentry>
