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);
     }
 }
        /// <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));
        }
        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 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 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;
     }
 }