简体   繁体   English

使用C#将XML转换为BSON

[英]XML to BSON using C#

I want to convert a XML file to BSON. 我想将XML文件转换为BSON。 then import the BSON to MongoDB. 然后将BSON导入MongoDB。 I searched but could not find how to covert this using C#. 我搜索但无法找到如何使用C#转换它。 please provide me a source code to do this using C# 请给我一个使用C#执行此操作的源代码

Had the same Problem today. 今天有同样的问题。 It's for sure not the best solution, but i solved it this way in my project and it works for what i need it: 这肯定不是最好的解决方案,但我在我的项目中以这种方式解决了它,它适用于我需要的东西:

  1. Deserialize XML to Json 将XML反序列化为Json
  2. Deserialize Json to Bson 将Json反序列化为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 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM