예제 #1
0
        /// <summary>
        /// Deserialize the object of the given xml-file.
        /// </summary>
        /// <param name="filepath">The xml-file I was talking about</param>
        /// <returns>A List containing all objects of the given file. Currently we should only have files with one object, though.</returns>
        private static List <object> Deserialize(string filepath)
        {
            var ser                = new NetDataContractSerializer();
            var fs                 = new FileStream(filepath, FileMode.Open);
            var reader             = XmlDictionaryReader.CreateTextReader(fs, XmlDictionaryReaderQuotas.Max);
            var deserializedObject = new List <object>();

            while (reader.Read())
            {
                if (ser.IsStartObject(reader))
                {
                    var o = ser.ReadObject(reader);
                    deserializedObject.Add(o);
                }
                break;
            }
            fs.Flush();
            fs.Close();

            return(deserializedObject);
        }
예제 #2
0
        //</snippet5>

        //<snippet6>
        public static void ReadObjectData(string path)
        {
            // Create the reader.
            FileStream          fs     = new FileStream(path, FileMode.Open);
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

            // Create the NetDataContractSerializer, specifying the type,
            // root, and namespace to use. The root value corresponds
            // to the DataContract.Name value, and the namespace value
            // corresponds to the DataContract.Namespace value.
            NetDataContractSerializer ser =
                new NetDataContractSerializer();

            // Test whether the serializer is on the start of the
            // object data. If so, read the data and write it
            // to the console.
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:

                    if (ser.IsStartObject(reader))
                    {
                        Console.WriteLine("Found the element");
                        Person p = (Person)ser.ReadObject(reader);
                        Console.WriteLine("{0} {1}    id:{2}",
                                          p.FirstName, p.LastName, p.ID);
                    }
                    Console.WriteLine(reader.Name);
                    break;
                }
            }
            fs.Flush();
            fs.Close();
        }