使用C#将XML转换为BSON [英] XML to BSON using C#

查看:73
本文介绍了使用C#将XML转换为BSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将XML文件转换为BSON.然后将BSON导入MongoDB.我进行了搜索,但找不到使用C#进行隐蔽的方法.请提供给我一个使用C#进行此操作的源代码

I want to convert a XML file to BSON. then import the BSON to MongoDB. I searched but could not find how to covert this using C#. please provide me a source code to do this using 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. 将XML反序列化为Json
  2. 将Json反序列化为Bson

  1. Deserialize XML to Json
  2. 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
 }

这篇关于使用C#将XML转换为BSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆