我想将一个XML文件转换成BSON格式,然后导入到MongoDB。我搜索了一下,但是找不到用C#来实现这个转换的方法。请提供一份用C#实现这个转换的源代码。
我想将一个XML文件转换成BSON格式,然后导入到MongoDB。我搜索了一下,但是找不到用C#来实现这个转换的方法。请提供一份用C#实现这个转换的源代码。
今天我遇到了同样的问题。虽然这不是最好的解决办法,但是我在我的项目中使用了这种方式解决了问题,并且它对我所需的内容有效:
Deserialize Json to Bson
using (var reader = new StreamReader(context.Request.Body))
{
var body = reader.ReadToEnd(); // read input string
XmlDocument doc = new XmlDocument();
doc.LoadXml(body); // String to XML Document
string jsonText = JsonConvert.SerializeXmlNode(doc); //XML to Json
var bsdocument = BsonSerializer.Deserialize<BsonDocument>(jsonText); //Deserialize JSON String to BSon Document
var mcollection = Program._database.GetCollection<BsonDocument>("test_collection_05");
await mcollection.InsertOneAsync(bsdocument); //Insert into mongoDB
}