private void Serialize(XmlWriter xmlWriter)
        {
            var customProperties = new PropertyBag();

            _adapter.Save(customProperties);
            CachingXmlSerializerFactory.Create(typeof(PropertyBag), new XmlRootAttribute("CustomProps")).SerializeWithoutDefaultNamespaces(xmlWriter, customProperties);
        }
        /// <summary>
        /// Function to serialize an object into XML
        /// </summary>
        /// <param name="objectToSerialize">Object to serialize</param>
        /// <param name="documentElementName">Element name</param>
        /// <returns></returns>
        public static XmlDocument SerializeToXml(Object objectToSerialize, string documentElementName)
        {
            XmlTypeAttribute xta = Attribute.GetCustomAttribute(objectToSerialize.GetType(), typeof(XmlTypeAttribute)) as XmlTypeAttribute;

            XmlDocument output = new XmlDocument();

            output.PreserveWhitespace = true;

            XmlRootAttribute xmlRoot = new XmlRootAttribute(documentElementName);

            xmlRoot.Namespace = xta.Namespace;

            XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

            xsn.Add("", "urn:hl7-org:v3");

            StringBuilder sb   = new StringBuilder();
            XmlWriter     xmlw = XmlWriter.Create(sb);

            //XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType(), xmlRoot); -- Use new CachingXmlSerializerFactory class
            var xmlSerializer = CachingXmlSerializerFactory.Create(objectToSerialize.GetType(), xmlRoot);

            xmlSerializer.Serialize(xmlw, objectToSerialize, xsn);
            xmlw.Close();

            output.LoadXml(sb.ToString());

            return(output);
        }
        public void TestCachingSerializer_Caching()
        {
            var ser1 = CachingXmlSerializerFactory.Create <List <int> >();

            var ser2 = CachingXmlSerializerFactory.Create <List <int> >();

            Assert.AreEqual(ser1, ser2);
        }
        //TEST
        /// <summary>
        ///     Deserialize XML string, optionally only an inner fragment of the XML, as specified by the innerStartTag parameter.
        /// </summary>
        public static T FromXml <T>(this XmlReader @this, string rootTag, string defaultNamespace = "")
        {
            var root = new XmlRootAttribute(rootTag)
            {
                Namespace = defaultNamespace
            };
            var xmlSerializer = CachingXmlSerializerFactory.Create(typeof(T), root);

            return((T)xmlSerializer.Deserialize(@this));
        }
 public static string ToXml <T>(this T @object)
 {
     using (var stringWriter = new StringWriter())
         using (var writer = XmlWriter.Create(stringWriter, new() { Indent = false, Encoding = Encoding.UTF8, OmitXmlDeclaration = true }))
         {
             var serializer = CachingXmlSerializerFactory.Create(typeof(T));
             serializer.SerializeWithoutDefaultNamespaces(writer, @object);
             return(stringWriter.ToString());
         }
 }
        protected string Serialize()
        {
            var builder = new StringBuilder();

            using (var writer = XmlWriter.Create(builder, new() { OmitXmlDeclaration = true }))
            {
                var serializer = CachingXmlSerializerFactory.Create(GetType());
                serializer.SerializeWithoutDefaultNamespaces(writer !, this);
            }
            return(builder.ToString());
        }
        public static T Clone <T>(this T source)
        {
            var serializer = CachingXmlSerializerFactory.Create <T>();

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.Serialize(ms, source);
                ms.Seek(0, SeekOrigin.Begin);
                return((T)serializer.Deserialize(ms));
            }
        }
Beispiel #8
0
        internal static Document LoadPolicyResourceDocument(string name)
        {
            // see Microsoft.BizTalk.PipelineEditor.PolicyFile.Document::Load, Microsoft.BizTalk.PipelineOM, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
            var xmlSchemas = (XmlSchemas)Reflector.GetProperty <Document>("SchemasForObjectModel");

            using (var stream = ResourceManager.Load(Assembly.GetExecutingAssembly(), $"{typeof(IStage).Namespace}.Resources.{name}"))
                using (var xmlReader = XmlReader.Create(stream, ValidatingXmlReaderSettings.Create(XmlSchemaContentProcessing.Strict, xmlSchemas.ToArray())))
                {
                    var xmlSerializer = CachingXmlSerializerFactory.Create(typeof(Document));
                    return((Document)xmlSerializer.Deserialize(xmlReader));
                }
        }
 //TEST
 public static string ToXml <T>(this T @this, string rootTag, string defaultNamespace, XmlSerializerNamespaces namespaces)
 {
     using (var stream = new MemoryStream()) {
         var root = new XmlRootAttribute(rootTag)
         {
             Namespace = defaultNamespace
         };
         var xmlSerializer = CachingXmlSerializerFactory.Create(typeof(T), root);
         xmlSerializer.Serialize(stream, @this, namespaces);
         stream.Position = 0;
         using (var reader = new StreamReader(stream)) {
             var result = reader.ReadToEnd();
             return(result);
         }
     }
 }
 /// <summary>
 ///     Deserialize XML string, optionally only an inner fragment of the XML, as specified by the innerStartTag parameter.
 /// </summary>
 public static T DeserializeXml <T>(this string @this, string innerStartTag = null)
 {
     using (var stringReader = new StringReader(@this))
     {
         using (var xmlReader = XmlReader.Create(stringReader))
         {
             if (innerStartTag != null)
             {
                 xmlReader.ReadToDescendant(innerStartTag);
                 var xmlSerializer = CachingXmlSerializerFactory.Create(typeof(T), new XmlRootAttribute(innerStartTag));
                 return((T)xmlSerializer.Deserialize(xmlReader.ReadSubtree()));
             }
             return((T)CachingXmlSerializerFactory.Create(typeof(T)).Deserialize(xmlReader));
         }
     }
 }
Beispiel #11
0
        public static void Serialize(this IMicroComponent component, XmlWriter writer)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }
            var overrides = new XmlAttributeOverrides();

            overrides.Add(component.GetType(), new() { XmlRoot = new(Constants.MICRO_COMPONENT_ELEMENT_NAME) });
            var serializer = CachingXmlSerializerFactory.Create(component.GetType(), overrides);

            using (var microComponentXmlWriter = new MicroComponentXmlWriter(writer, component))
            {
                serializer.SerializeWithoutDefaultNamespaces(microComponentXmlWriter, component);
            }
        }
        protected override XmlSerializer CreateXmlSerializer()
        {
            var overrides = new XmlAttributeOverrides();

            overrides.Ignore <Document>(d => d.PolicyFilePath);
            overrides.Add <Document>(d => d.CategoryId, new XmlAttributes {
                XmlElements = { new XmlElementAttribute(nameof(Document.CategoryId), typeof(string)) }
            });
            overrides.Add <Document>(d => d.FriendlyName, new XmlAttributes {
                XmlElements = { new XmlElementAttribute(nameof(Document.FriendlyName), typeof(string)) }
            });
            overrides.Ignore <PolicyFileStage>(s => s.CategoryId);
            overrides.Add <PolicyFileStage>(
                s => s.PolicyFileStage,
                new XmlAttributes {
                XmlElements = { new XmlElementAttribute(nameof(PolicyFileStage.PolicyFileStage), typeof(Microsoft.BizTalk.PipelineEditor.PolicyFile.Stage)) }
            });
            return(CachingXmlSerializerFactory.Create(typeof(Document), overrides));
        }
Beispiel #13
0
        public static IMicroComponent DeserializeMicroPipelineComponent(this XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            reader.AssertStartElement(Constants.MICRO_COMPONENT_ELEMENT_NAME);
            var microPipelineComponentType = Type.GetType(reader.GetMandatoryAttribute(Constants.MICRO_COMPONENT_TYPE_ATTRIBUTE_NAME), true);

            if (!typeof(IMicroComponent).IsAssignableFrom(microPipelineComponentType))
            {
                throw new ConfigurationErrorsException($"{microPipelineComponentType.AssemblyQualifiedName} does not implement {nameof(IMicroComponent)}.");
            }

            // reset position to an element as required to call ReadSubtree()
            reader.MoveToElement();
            var microPipelineComponentXmlSubtree = reader.ReadSubtree();

            IMicroComponent component;

            if (typeof(IXmlSerializable).IsAssignableFrom(microPipelineComponentType))
            {
                component = (IMicroComponent)Activator.CreateInstance(microPipelineComponentType);
                // relieve micro pipeline components from having to deal with surrounding mComponent XML element
                microPipelineComponentXmlSubtree.MoveToContent();
                reader.ReadStartElement(Constants.MICRO_COMPONENT_ELEMENT_NAME);
                ((IXmlSerializable)component).ReadXml(microPipelineComponentXmlSubtree);
            }
            else
            {
                var overrides = new XmlAttributeOverrides();
                overrides.Add(microPipelineComponentType, new XmlAttributes {
                    XmlRoot = new XmlRootAttribute(Constants.MICRO_COMPONENT_ELEMENT_NAME)
                });
                var serializer = CachingXmlSerializerFactory.Create(microPipelineComponentType, overrides);
                component = (IMicroComponent)serializer.Deserialize(microPipelineComponentXmlSubtree);
            }

            reader.Skip();
            return(component);
        }
Beispiel #14
0
        private XmlSerializer CreateXmlSerializer()
        {
            var overrides = new XmlAttributeOverrides();

            overrides.Add <Document>(new() { XmlType = new("Root") });
            overrides.Ignore <Document>(d => d.Description);
            overrides.Ignore <Document>(d => d.MajorVersion);
            overrides.Ignore <Document>(d => d.MinorVersion);
            overrides.Ignore <Document>(d => d.PolicyFilePath);

            overrides.Add <Stage>(s => s.Components, new() { XmlArrayItems = { new XmlArrayItemAttribute("Component", typeof(ComponentBinding)) } });

            overrides.Add <ComponentInfo>(ci => ci.QualifiedNameOrClassId, new() { XmlAttribute = new("Name") });
            overrides.Ignore <ComponentInfo>(ci => ci.CachedDisplayName);
            overrides.Ignore <ComponentInfo>(ci => ci.CachedIsManaged);
            overrides.Ignore <ComponentInfo>(ci => ci.ComponentName);
            overrides.Ignore <ComponentInfo>(ci => ci.ComponentProperties);
            overrides.Ignore <ComponentInfo>(ci => ci.Description);
            overrides.Ignore <ComponentInfo>(ci => ci.Version);
            return(CachingXmlSerializerFactory.Create(typeof(Document), overrides));
        }
Beispiel #15
0
        /// <summary>
        /// Converts a PCEHR header into a common PCEHR header that can be validated.
        /// </summary>
        /// <typeparam name="T">PCEHR header type.</typeparam>
        /// <param name="header">PCEHR header.</param>
        /// <returns>Converted header.</returns>
        private static PCEHRHeader ConvertPcehrHeader <T>(T header)
        {
            Debug.Assert(typeof(T).IsClass);
            Debug.Assert(typeof(T).Name == "PCEHRHeader");

            // Serialize
            MemoryStream memoryStream = new MemoryStream();
            //Updated to use memory safe version
            //XmlSerializer serializer = new XmlSerializer(typeof(T));
            XmlSerializer serializer = CachingXmlSerializerFactory.Create(typeof(T));

            serializer.Serialize(memoryStream, header);
            memoryStream.Position = 0;

            // Deserialize
            //Updated to use memory safe version
            //XmlSerializer deserializer = new XmlSerializer(typeof(PCEHRHeader));
            XmlSerializer deserializer = CachingXmlSerializerFactory.Create(typeof(PCEHRHeader));
            PCEHRHeader   pcehrHeader  = (PCEHRHeader)deserializer.Deserialize(memoryStream);

            return(pcehrHeader);
        }
Beispiel #16
0
 private void Serialize(XmlWriter xmlWriter)
 {
     CachingXmlSerializerFactory.Create(typeof(BindingInfo)).Serialize(xmlWriter, _applicationBinding.Accept(new BindingInfoBuilder()).BindingInfo);
 }
 protected override XmlSerializer CreateXmlSerializer()
 {
     return(CachingXmlSerializerFactory.Create(typeof(Document)));
 }