gpt4 book ai didi

c# - 使用 C# 将 XML 转换为 BSON

转载 作者:可可西里 更新时间:2023-11-01 09:44:35 26 4
gpt4 key购买 nike

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

最佳答案

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

  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
    }

关于c# - 使用 C# 将 XML 转换为 BSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com