Пример #1
0
        /// <summary>
        /// Serializes an object, or graph of objects with the given root to the provided stream.
        /// </summary>
        /// <param name="serializationStream">The stream where the formatter puts the serialized data.
        /// This stream can reference a variety of backing stores (such as files, network, memory, and so on).</param>
        /// <param name="graph">The object, or root of the object graph, to serialize.
        /// All child objects of this root object are automatically serialized.</param>
        public void Serialize(System.IO.Stream serializationStream, object graph)
        {
            XmlTextWriter writer = new XmlTextWriter(serializationStream, null);

            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("Data");
            Dictionary <object, bool> savedObjects = new Dictionary <object, bool>();

            if (PersistenceData == null)
            {
                PersistenceData = new XmlFormatterPersistenceData();
            }
            writer.WriteStartElement("Object");
            SerializeObject(writer, graph, savedObjects);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
Пример #2
0
        /// <summary>
        /// Deserializes the data on the provided stream and reconstitutes the graph of objects.
        /// </summary>
        /// <param name="serializationStream">The stream that contains the data to deserialize.</param>
        /// <returns>The top object of the deserialized graph.</returns>
        public object Deserialize(System.IO.Stream serializationStream)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            //First the xml document is loaded
            XmlDocument doc = new XmlDocument();

            doc.Load(serializationStream);

            //Then we read it into a structure
            Dictionary <String, INode> objects = new Dictionary <String, INode>();
            INode graph = ParseNode(doc.DocumentElement.ChildNodes[0], objects, ErrorCallback);

            //And finally we build the object graph and return it
            PersistenceData = new XmlFormatterPersistenceData();
            object o = graph.GetObject(null, objects);

            if (o == null)
            {
                ErrorCallback("Unable to create root object");
            }

            return(o);
        }
 /// <summary>
 /// Serializes an object, or graph of objects with the given root to the provided stream.
 /// </summary>
 /// <param name="serializationStream">The stream where the formatter puts the serialized data. 
 /// This stream can reference a variety of backing stores (such as files, network, memory, and so on).</param>
 /// <param name="graph">The object, or root of the object graph, to serialize. 
 /// All child objects of this root object are automatically serialized.</param>
 public void Serialize(System.IO.Stream serializationStream, object graph)
 {
     XmlTextWriter writer = new XmlTextWriter(serializationStream, null);
     writer.Formatting = Formatting.Indented;
     writer.WriteStartDocument();
     writer.WriteStartElement("Data");
     Dictionary<object, bool> savedObjects = new Dictionary<object, bool>();
     if (PersistenceData == null) PersistenceData = new XmlFormatterPersistenceData();
     writer.WriteStartElement("Object");
     SerializeObject(writer, graph, savedObjects);
     writer.WriteEndElement();
     writer.WriteEndElement();
     writer.WriteEndDocument();
     writer.Flush();
 }
        /// <summary>
        /// Deserializes the data on the provided stream and reconstitutes the graph of objects.
        /// </summary>
        /// <param name="serializationStream">The stream that contains the data to deserialize.</param>
        /// <returns>The top object of the deserialized graph.</returns>
        public object Deserialize(System.IO.Stream serializationStream)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            //First the xml document is loaded
            XmlDocument doc = new XmlDocument();
            doc.Load(serializationStream);

            //Then we read it into a structure
            Dictionary<String, INode> objects = new Dictionary<String, INode>();
            INode graph = ParseNode(doc.DocumentElement.ChildNodes[0], objects, ErrorCallback);

            //And finally we build the object graph and return it
            PersistenceData = new XmlFormatterPersistenceData();
            object o = graph.GetObject(null, objects);
            if (o == null)
                ErrorCallback("Unable to create root object");

            return o;
        }