/// <summary>
 /// Reads an object. The reader must be positioned on the element name.
 /// </summary>
 /// <param name="sr">This <see cref="IStructuredReader"/> object.</param>
 /// <param name="elementName">Name of the xml element.</param>
 /// <returns>Object read.</returns>
 static public object ReadObjectElement( this IStructuredReader sr, string elementName )
 {
     // This does the trick: it raises an XmlException.
     if( !sr.Xml.IsStartElement( elementName ) ) sr.Xml.ReadStartElement( elementName );
     StandardReadStatus status;
     object o = sr.ReadInlineObject( out status );
     if( (status & StandardReadStatus.ErrorMask) != 0 ) throw new CKException( R.UnableToReadInlineObject );
     return o;
 }
 /// <summary>
 /// Reads an object. The reader must be positioned on the element name.
 /// </summary>
 /// <param name="sr">This <see cref="IStructuredReader"/> object.</param>
 /// <param name="elementName">Name of the xml element.</param>
 /// <param name="o">The object read. Null on error.</param>
 /// <returns>A <see cref="StandardReadStatus"/> that may define an error.</returns>
 static public StandardReadStatus ReadObjectElement( this IStructuredReader sr, string elementName, out object o )
 {
     // This does the trick: it raises an XmlException.
     if( !sr.Xml.IsStartElement( elementName ) )
     {
         o = null; 
         return StandardReadStatus.ErrorXmlRead;
     }
     StandardReadStatus status;
     o = sr.ReadInlineObject( out status );
     return status;
 }
Beispiel #3
0
        static ReadElementObjectInfo ReadObjectInfo(IStructuredReader sr)
        {
            Debug.Assert(sr.Xml.Name == "data");

            object o            = null;
            string errorMessage = "Missing or empty 'key' attribute.";

            ReadElementObjectInfo.ReadStatus status = ReadElementObjectInfo.ReadStatus.ErrorKeyAttributeMissing;
            string key = sr.Xml.GetAttribute("key");

            bool readDone = false;

            if (key != null)
            {
                key = key.Trim();
                if (key.Length > 0)
                {
                    try
                    {
                        readDone = true;
                        StandardReadStatus readStatus;
                        o      = sr.ReadInlineObject(out readStatus);
                        status = (ReadElementObjectInfo.ReadStatus)readStatus;
                    }
                    catch (Exception ex)
                    {
                        status      |= ReadElementObjectInfo.ReadStatus.ErrorWhileReadingElementObject;
                        errorMessage = ex.Message;
                    }
                }
            }
            if ((status & ReadElementObjectInfo.ReadStatus.ErrorMask) == 0)
            {
                return(new ReadElementObjectInfo(status, key, o));
            }
            if (!readDone)
            {
                sr.Xml.Skip();
            }
            return(new ReadElementObjectInfo(status, sr.Xml, errorMessage));
        }
Beispiel #4
0
        public void SimpleStructuredSerializerTest()
        {
            string testPath = TestBase.GetTestFilePath("CKTests.Storage", "StructuredSerializer");
            IStructuredSerializer <Dog> serializer = new SimpleStructuredDogSerializer();
            Dog dog = CreateDog();

            using (Stream str = new FileStream(testPath, FileMode.Create))
            {
                using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(str, new SimpleServiceContainer()))
                {
                    // This is an independant scope: we just created the writer...
                    writer.ServiceContainer.Add <UniqueService>(new UniqueService());
                    writer.ServiceContainer.Add <IStructuredSerializer <Dog> >(new SimpleStructuredDogSerializer());
                    writer.ServiceContainer.Add <IStructuredSerializer <Paw> >(new SimpleStructuredPawSerializer());

                    writer.WriteObjectElement("Dog", dog);
                }
            }

            Dog readDog;

            // 1 - Use ReadInlineObject
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    StandardReadStatus status;
                    object             o = reader.ReadInlineObject(out status);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 2 - Use ReadInlineObjectStructured( Type )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for the Dog type.
                    object o = reader.ReadInlineObjectStructured(typeof(Dog), null);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 3 - Use ReadInlineObjectStructured( object )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    readDog = new Dog();
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for an empty Dog object.
                    object o = reader.ReadInlineObjectStructured(readDog);
                }
            CheckReadDog(readDog);
        }