byte[] binaryData; using (var reader = new BsonReader(new MemoryStream())) { reader.ReadStartDocument(); reader.ReadBinaryData("fieldName", out int binaryDataSubType, out int binaryDataLength, out binaryData); reader.ReadEndDocument(); }
byte[] binaryData = null; using (var stream = new MemoryStream()) { using (var writer = new BsonBinaryWriter(stream)) { writer.WriteStartDocument(); writer.WriteBinaryData("fieldName", new byte[] { 0x01, 0x02, 0x03 }, BsonBinarySubType.Generic); writer.WriteEndDocument(); writer.Flush(); } stream.Position = 0; using (var reader = new BsonReader(stream)) { reader.ReadStartDocument(); reader.ReadBinaryData("fieldName", out int binaryDataSubType, out int binaryDataLength, out binaryData); reader.ReadEndDocument(); } }In this example, we create a new MemoryStream object and use a BsonBinaryWriter to write a BSON document with a binary data field with the field name "fieldName" and binary data of {0x01, 0x02, 0x03}. We then use a BsonReader to read the binary data field from the MemoryStream and store it in the binaryData array. Both of these examples use the MongoDB.Bson library.