using MongoDB.Bson; using MongoDB.Bson.IO; // Read the BSON document from a file using (var stream = new FileStream("document.bson", FileMode.Open)) using (var reader = new BsonBinaryReader(stream)) { BsonDocument document = BsonSerializer.Deserialize(reader); // Do something with the document }
using MongoDB.Bson; using MongoDB.Bson.IO; // Read the BSON document from a MemoryStream byte[] bsonBytes = // Get the BSON bytes from somewhere using (var stream = new MemoryStream(bsonBytes)) using (var reader = new BsonBinaryReader(stream)) { BsonDocument document = BsonSerializer.DeserializeThis code reads a BSON document from a byte array using a MemoryStream and BsonBinaryReader, then deserializes the document into a BsonDocument object using the BsonSerializer. Package library: MongoDB.Bson.dll (MongoDB C# Driver)(reader); // Do something with the document }