Book a Demo

Author Topic: Serialize Repository using C#  (Read 6208 times)

jafuentes

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Serialize Repository using C#
« on: October 26, 2010, 02:50:33 am »
Hello,

I´ve following problem. I´ve a model that i need to serialize. I´m using EA8 and VS2010/C#. In an Addin, i call the method SerializeObject:

Code: [Select]
private String UTF8ByteArrayToString(Byte[] characters)
{
    UTF8Encoding encoding = new UTF8Encoding();
    String constructedString = encoding.GetString(characters);
    return (constructedString);
}

private Byte[] StringToUTF8ByteArray(String pXmlString)
{
   UTF8Encoding encoding = new UTF8Encoding();
   Byte[] byteArray = encoding.GetBytes(pXmlString);
   return byteArray;
}

public String SerializeObject(Repository rep)
{
 try{
      String XmlizedString = null;
      MemoryStream memoryStream = new MemoryStream();
      XmlSerializer xs = new XmlSerializer(typeof(rep.GetType()));
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

      xs.Serialize(xmlTextWriter, rep);
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
      XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
      return XmlizedString;
  }
  catch (Exception e){
      System.Console.WriteLine(e);
      return null;
  }
}

when executed this line:

Code: [Select]
XmlSerializer xs = new XmlSerializer(rep.GetType());

And exception ocurrs. The error is:

Code: [Select]
$exception      {"Unable to generate a temporary class (result=1).\r\nerror CS0122: 'System.__ComObject' is inaccessible due to its protection level\r\nerror CS0051: Inconsistent accessibility: parameter type 'System.__ComObject' is less accessible than method 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter__ComObject.Write3___ComObject(string, string, System.__ComObject, bool, bool)'\r\nerror CS0122: 'System.__ComObject' is inaccessible due to its protection level\r\nerror CS0050: Inconsistent accessibility: return type 'System.__ComObject' is less accessible than method 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader__ComObject.Read3___ComObject(bool, bool)'\r\n"}      System.Exception {System.InvalidOperationException}

How I can serialize rep?

Thanks. Regards

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Serialize Repository using C#
« Reply #1 on: October 26, 2010, 09:49:55 am »
EA automation objects don't support serialization.

What you probably want is to export to xmi.  (Which is available from the project interface from memory.)