コード例 #1
0
ファイル: ExtensionMethods.cs プロジェクト: psulek/doemd
        public static string SerializeToString(this ModelElement modelElement)
        {
            string serializedString = null;
            var    serializer       = modelElement.GetSerializer();

            if (serializer != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    DomainXmlSerializerDirectory directory            = modelElement.Store.SerializerDirectory;
                    SerializationContext         serializationContext = new SerializationContext(directory);

                    // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.
                    serializationContext.WriteOptionalPropertiesWithDefaultValue = false;
                    var settings =
                        DONetEntityModelDesignerSerializationHelper.Instance.CreateXmlWriterSettings(serializationContext,
                                                                                                     false,
                                                                                                     Encoding.UTF8);

                    using (XmlWriter writer = XmlWriter.Create(stream, settings))
                    {
                        serializer.Write(serializationContext, modelElement, writer);

                        writer.Flush();

                        stream.Position  = 0;
                        serializedString = new StreamReader(stream).ReadToEnd();
                        writer.Close();
                        stream.Close();
                    }
                }
            }
            return(serializedString);
        }