MongoDB.Bson.IO.BsonBinaryWriter.WriteStartDocument C# (CSharp) Метод

WriteStartDocument() публичный Метод

Writes the start of a BSON document to the writer.
public WriteStartDocument ( ) : void
Результат void
        public override void WriteStartDocument()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
            if (State != BsonWriterState.Initial && State != BsonWriterState.Value && State != BsonWriterState.ScopeDocument && State != BsonWriterState.Done)
            {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Initial, BsonWriterState.Value, BsonWriterState.ScopeDocument, BsonWriterState.Done);
            }

            base.WriteStartDocument();
            if (State == BsonWriterState.Value)
            {
                _buffer.WriteByte((byte)BsonType.Document);
                WriteNameHelper();
            }
	        _context = new BsonBinaryWriterContext(_context, ContextType.Document, _buffer.Position);
            _buffer.WriteInt32(0); // reserve space for size

            State = BsonWriterState.Name;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Reads a raw BSON array.
        /// </summary>
        /// <returns>The raw BSON array.</returns>
        public virtual IByteBuffer ReadRawBsonArray()
        {
            // overridden in BsonBinaryReader to read the raw bytes from the stream
            // for all other streams, deserialize the array and reserialize it using a BsonBinaryWriter to get the raw bytes

            var deserializationContext = BsonDeserializationContext.CreateRoot(this);
            var array = BsonArraySerializer.Instance.Deserialize(deserializationContext);

            using (var memoryStream = new MemoryStream())
                using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults))
                {
                    var serializationContext = BsonSerializationContext.CreateRoot(bsonWriter);
                    bsonWriter.WriteStartDocument();
                    var startPosition = memoryStream.Position + 3; // just past BsonType, "x" and null byte
                    bsonWriter.WriteName("x");
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                    var endPosition = memoryStream.Position;
                    bsonWriter.WriteEndDocument();

                    byte[] memoryStreamBuffer;
#if NETSTANDARD1_5 || NETSTANDARD1_6
                    memoryStreamBuffer = memoryStream.ToArray();
#else
                    memoryStreamBuffer = memoryStream.GetBuffer();
#endif
                    var buffer = new ByteArrayBuffer(memoryStreamBuffer, (int)memoryStream.Length, isReadOnly: true);
                    return(new ByteBufferSlice(buffer, (int)startPosition, (int)(endPosition - startPosition)));
                }
        }
All Usage Examples Of MongoDB.Bson.IO.BsonBinaryWriter::WriteStartDocument