public static bool WriteToXml(object inputDataset, string xmlFilePath)
 {
     try
     {
         // Check if file exists
         if (File.Exists(xmlFilePath))
         {
             Console.WriteLine("File already exists.");
             return(false);
         }
         // Create new file.
         IFile xmlFile = new FileStreamClass();
         xmlFile.Open(xmlFilePath, esriFilePermission.esriReadWrite);
         // See if the input dataset can be Xml serialized.
         IXMLSerialize mySerializeData = (IXMLSerialize)inputDataset;
         // Create new XmlWriter object.
         IXMLWriter myXmlWriter = new XMLWriterClass();
         myXmlWriter.WriteTo((IStream)xmlFile);
         myXmlWriter.WriteXMLDeclaration();
         IXMLSerializer myXmlSerializer = new XMLSerializerClass();
         // Write to XML File
         myXmlSerializer.WriteObject(myXmlWriter, null, null, null, null, mySerializeData);
         Console.WriteLine("Success.");
         return(true);
     }
     catch (Exception exc)
     {
         Console.WriteLine("Exception caught in WriteToXml: " + exc.Message);
         Console.WriteLine("Failed.");
         return(false);
     }
 }
    unsafe public static object Load(System.Runtime.InteropServices.ComTypes.IStream stream)
    {
      // Exit if Stream is NULL
      if (stream == null) { return null; }

      // Get Pointer to Int32
      int cb;
      int* pcb = &cb;

      // Get Size of the object's Byte Array
      byte[] arrLen = new Byte[4];
      stream.Read(arrLen, arrLen.Length, new IntPtr(pcb));
      cb = BitConverter.ToInt32(arrLen, 0);

      // Read the object's Byte Array
      byte[] bytes = new byte[cb];
      stream.Read(bytes, cb, new IntPtr(pcb));

      if (bytes.Length != cb)
        throw new Exception("Error reading object from stream");

      // Deserialize byte array
      AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(PeristStreamHelper.ResolveEventHandler);
      object data = null;
      MemoryStream memoryStream = new MemoryStream(bytes);
      BinaryFormatter binaryFormatter = new BinaryFormatter();
      object objectDeserialize = binaryFormatter.Deserialize(memoryStream);
      if (objectDeserialize != null)
      {
        data = objectDeserialize;
      }
      memoryStream.Close();
      AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(PeristStreamHelper.ResolveEventHandler);

      //deserialize ArcObjects
      if (data is string)
      {
        string str = (string)data;
        if (str.IndexOf("http://www.esri.com/schemas/ArcGIS/9.2") != -1)
        {
          IXMLStream readerStream = new XMLStreamClass();
          readerStream.LoadFromString(str);

          IXMLReader xmlReader = new XMLReaderClass();
          xmlReader.ReadFrom((IStream)readerStream);

          IXMLSerializer xmlReadSerializer = new XMLSerializerClass();
          object retObj = xmlReadSerializer.ReadObject(xmlReader, null, null);
          if (null != retObj)
            data = retObj;
        }
      }

      return data;
    }
        /// <summary>
        /// Serializes an object using ArcObjects xml serialization into a BSON Element
        /// Used by the CatalogDataset to store metadata
        /// </summary>
        /// <param name="ipItem">The object to serialize</param>
        /// <returns>The BSON element containing the bytes</returns>
        internal static BsonValue ObjectToBson(System.Object ipItem)
        {
            IXMLStream ipXmlStream = new XMLStreamClass();
            IXMLWriter ipXmlWriter = new XMLWriterClass();

            ipXmlWriter.WriteTo((IStream)ipXmlStream);
            IXMLSerializer ipXmlSer = new XMLSerializerClass();

            ipXmlSer.WriteObject(ipXmlWriter, null, null, "Test", "Test", ipItem);
            byte[] bytes = ipXmlStream.SaveToBytes();
            return(BsonValue.Create(bytes));
        }
Beispiel #4
0
        public static IGeometry ReadGeometryFromXml(string filePath)
        {
            IXMLSerializer serializer = new XMLSerializerClass();

            IXMLReader reader = new XMLReaderClass();

            IXMLStream stream = new XMLStreamClass();

            stream.LoadFromFile(filePath);

            reader.ReadFrom((IStream)stream);

            return((IGeometry)serializer.ReadObject(reader, null, null));
        }
        private static object ReadGeometryFromXML(string filePath)
        {
            IXMLSerializer serializer = new XMLSerializerClass();

            IXMLReader reader = new XMLReaderClass();

            IXMLStream stream = new XMLStreamClass();

            stream.LoadFromFile(filePath);

            reader.ReadFrom((IStream)stream);

            return(serializer.ReadObject(reader, null, null));
        }
 /// <summary>
 /// Gets an object serialized using ArcObjects xml serialization from a BSON Element
 /// Used by the CatalogDataset to extract metadata
 /// </summary>
 /// <param name="byteStuff">The BSON element containing the bytes</param>
 /// <returns>The object deserialized</returns>
 internal static System.Object BsonToObject(BsonElement byteStuff)
 {
     try
     {
         byte[]     bytes       = byteStuff.Value.AsByteArray;
         IXMLStream ipXmlStream = new XMLStreamClass();
         ipXmlStream.LoadFromBytes(ref bytes);
         IXMLReader ipXmlReader = new XMLReaderClass();
         ipXmlReader.ReadFrom((IStream)ipXmlStream);
         IXMLSerializer ipXmlSer = new XMLSerializerClass();
         return(ipXmlSer.ReadObject(ipXmlReader, null, null));
     }
     catch (Exception)
     {
         throw new COMException("Value expected as byte array isn't");
     }
 }
 public static object ReadFromXml(string xmlFilePath)
 {
     try
     {
         IFile inputXmlFile = new FileStreamClass();
         inputXmlFile.Open(xmlFilePath, esriFilePermission.esriReadWrite);
         IXMLReader myXmlReader = new XMLReaderClass();
         myXmlReader.ReadFrom((IStream)inputXmlFile);
         IXMLSerializer myInputXmlSerializer = new XMLSerializerClass();
         object         myFunctionObject     = myInputXmlSerializer.ReadObject(myXmlReader, null, null);
         return(myFunctionObject);
     }
     catch (Exception exc)
     {
         Console.WriteLine("Exception caught in ReadFromXml: " + exc.Message);
         return(null);
     }
 }
        unsafe public static void Save(System.Runtime.InteropServices.ComTypes.IStream stream, object data)
        {
            // Exit if Stream or DataTable is NULL
            if (stream == null)
            {
                return;
            }
            if (data == null)
            {
                return;
            }

            //COM objects needs special care...
            if (Marshal.IsComObject(data))
            {
                //*** Create XmlWriter ***
                IXMLWriter xmlWriter = new XMLWriterClass();

                //*** Create XmlStream ***
                IXMLStream xmlStream = new XMLStreamClass();

                //*** Write the object to the stream ***
                xmlWriter.WriteTo(xmlStream as IStream);

                //*** Serialize object ***
                IXMLSerializer xmlSerializer = new XMLSerializerClass();
                xmlSerializer.WriteObject(xmlWriter, null, null, "arcobject", "http://www.esri.com/schemas/ArcGIS/9.2", data);

                string str = xmlStream.SaveToString();

                data = (object)str;
                if (null == data)
                {
                    return;
                }
            }

            //make sure that the object is serializable
            if (!data.GetType().IsSerializable)
            {
                throw new Exception("Object is not serializable.");
            }

            // Convert the string into a byte array
            MemoryStream    memoryStream    = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(memoryStream, data);
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            // Get Memory Pointer to Int32
            int  cb;
            int *pcb = &cb;

            // write the object to the structured stream
            byte[] arrLen = BitConverter.GetBytes(bytes.Length);  // Get Byte Length
            stream.Write(arrLen, arrLen.Length, new IntPtr(pcb)); // Write Byte Length
            stream.Write(bytes, bytes.Length, new IntPtr(pcb));   // Write Btye Array

            if (bytes.Length != cb)
            {
                throw new Exception("Error writing object to stream");
            }
        }
        unsafe public static object Load(System.Runtime.InteropServices.ComTypes.IStream stream)
        {
            // Exit if Stream is NULL
            if (stream == null)
            {
                return(null);
            }

            // Get Pointer to Int32
            int  cb;
            int *pcb = &cb;

            // Get Size of the object's Byte Array
            byte[] arrLen = new Byte[4];
            stream.Read(arrLen, arrLen.Length, new IntPtr(pcb));
            cb = BitConverter.ToInt32(arrLen, 0);

            // Read the object's Byte Array
            byte[] bytes = new byte[cb];
            stream.Read(bytes, cb, new IntPtr(pcb));

            if (bytes.Length != cb)
            {
                throw new Exception("Error reading object from stream");
            }

            // Deserialize byte array
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(PeristStreamHelper.ResolveEventHandler);
            object          data              = null;
            MemoryStream    memoryStream      = new MemoryStream(bytes);
            BinaryFormatter binaryFormatter   = new BinaryFormatter();
            object          objectDeserialize = binaryFormatter.Deserialize(memoryStream);

            if (objectDeserialize != null)
            {
                data = objectDeserialize;
            }
            memoryStream.Close();
            AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(PeristStreamHelper.ResolveEventHandler);

            //deserialize ArcObjects
            if (data is string)
            {
                string str = (string)data;
                if (str.IndexOf("http://www.esri.com/schemas/ArcGIS/9.2") != -1)
                {
                    IXMLStream readerStream = new XMLStreamClass();
                    readerStream.LoadFromString(str);

                    IXMLReader xmlReader = new XMLReaderClass();
                    xmlReader.ReadFrom((IStream)readerStream);

                    IXMLSerializer xmlReadSerializer = new XMLSerializerClass();
                    object         retObj            = xmlReadSerializer.ReadObject(xmlReader, null, null);
                    if (null != retObj)
                    {
                        data = retObj;
                    }
                }
            }

            return(data);
        }
    unsafe public static void Save(System.Runtime.InteropServices.ComTypes.IStream stream, object data)
    {
      // Exit if Stream or DataTable is NULL
      if (stream == null) { return; }
      if (data == null) { return; }

      //COM objects needs special care...
      if (Marshal.IsComObject(data))
      {
        //*** Create XmlWriter ***
        IXMLWriter xmlWriter = new XMLWriterClass();

        //*** Create XmlStream ***
        IXMLStream xmlStream = new XMLStreamClass();

        //*** Write the object to the stream ***
        xmlWriter.WriteTo(xmlStream as IStream);

        //*** Serialize object ***
        IXMLSerializer xmlSerializer = new XMLSerializerClass();
        xmlSerializer.WriteObject(xmlWriter, null, null, "arcobject", "http://www.esri.com/schemas/ArcGIS/9.2", data);

        string str = xmlStream.SaveToString();

        data = (object)str;
        if (null == data)
          return;
      }

      //make sure that the object is serializable
      if (!data.GetType().IsSerializable)
        throw new Exception("Object is not serializable.");
      
      // Convert the string into a byte array
      MemoryStream memoryStream = new MemoryStream();
      BinaryFormatter binaryFormatter = new BinaryFormatter();
      binaryFormatter.Serialize(memoryStream, data);
      byte[] bytes = memoryStream.ToArray();
      memoryStream.Close();

      // Get Memory Pointer to Int32
      int cb;
      int* pcb = &cb;

      // write the object to the structured stream
      byte[] arrLen = BitConverter.GetBytes(bytes.Length);  // Get Byte Length
      stream.Write(arrLen, arrLen.Length, new IntPtr(pcb));   // Write Byte Length
      stream.Write(bytes, bytes.Length, new IntPtr(pcb));     // Write Btye Array

      if (bytes.Length != cb)
        throw new Exception("Error writing object to stream");
    }
 public static object ReadFromXml(string xmlFilePath)
 {
     try
     {
         IFile inputXmlFile = new FileStreamClass();
         inputXmlFile.Open(xmlFilePath, esriFilePermission.esriReadWrite);
         IXMLReader myXmlReader = new XMLReaderClass();
         myXmlReader.ReadFrom((IStream)inputXmlFile);
         IXMLSerializer myInputXmlSerializer = new XMLSerializerClass();
         object myFunctionObject = myInputXmlSerializer.ReadObject(myXmlReader, null, null);
         return myFunctionObject;
     }
     catch (Exception exc)
     {
         Console.WriteLine("Exception caught in ReadFromXml: " + exc.Message);
         return null;
     }
 }
 public static bool WriteToXml(object inputDataset, string xmlFilePath)
 {
     try
     {
         // Check if file exists
         if (File.Exists(xmlFilePath))
         {
             Console.WriteLine("File already exists.");
             return false;
         }
         // Create new file.
         IFile xmlFile = new FileStreamClass();
         xmlFile.Open(xmlFilePath, esriFilePermission.esriReadWrite);
         // See if the input dataset can be Xml serialized.
         IXMLSerialize mySerializeData = (IXMLSerialize)inputDataset;
         // Create new XmlWriter object.
         IXMLWriter myXmlWriter = new XMLWriterClass();
         myXmlWriter.WriteTo((IStream)xmlFile);
         myXmlWriter.WriteXMLDeclaration();
         IXMLSerializer myXmlSerializer = new XMLSerializerClass();
         // Write to XML File
         myXmlSerializer.WriteObject(myXmlWriter, null, null, null, null, mySerializeData);
         Console.WriteLine("Success.");
         return true;
     }
     catch (Exception exc)
     {
         Console.WriteLine("Exception caught in WriteToXml: " + exc.Message);
         Console.WriteLine("Failed.");
         return false;
     }
 }