【问题标题】:XML to BSON using C#使用 C# 将 XML 转换为 BSON
【发布时间】:2014-05-31 04:07:06
【问题描述】:

我想将一个 XML 文件转换为 BSON。然后将 BSON 导入 MongoDB。我搜索但找不到如何使用 C# 来隐藏它。请提供我使用 C# 执行此操作的源代码

【问题讨论】:

  • 查看接受的答案here,另请参考XML to JSON in c#
  • 将您的 XML 反序列化为 c# 对象。然后,使用 MongoDb 驱动程序将该对象序列化为集合。最简单的就是在C#类中添加属性来控制序列化过程。在您尝试之后,如果它不起作用,请发布更多细节,以便我们提供更好的帮助。

标签: c# xml mongodb export bson


【解决方案1】:

今天遇到了同样的问题。 这肯定不是最好的解决方案,但 我在我的项目中以这种方式解决了它,它可以满足我的需要:

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

【讨论】:

    猜你喜欢
    • 2019-05-06
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多