[英]Converting XML to BsonArray in MongoDB using C#
I have an XML file and I want to convert to BsonArray in MongoDB, later then I can make it a list of elements.我有一个 XML 文件,我想在 MongoDB 中转换为 BsonArray,然后我可以将其设为元素列表。 Here is what I tried.
这是我尝试过的。
XmlDocument doc = new XmlDocument();
doc.Load("Books.xml");
string json_doc = JsonConvert.SerializeXmlNode(doc);
...
using (var jsonReader = new JsonReader(json_doc))
{
var context = BsonDeserializationContext.CreateRoot(jsonReader);
var document = XML_collection.DocumentSerializer.Deserialize(context);
}
Code converts XML to Json but not BsonArray.代码将 XML 转换为 Json 但不是 BsonArray。 That means what I will get is only ONE document with hundreds of fields.
这意味着我将得到的只是一份包含数百个字段的文档。 But what I want is making them separate as a list.
但我想要的是把它们分开作为一个列表。
You can use BsonDocument.Parse()
to read your string and then you can access catalog.book
path to cast into BsonArray
:您可以使用
BsonDocument.Parse()
来读取您的字符串,然后您可以访问catalog.book
路径以转换为BsonArray
:
var json_doc = JsonConvert.SerializeXmlNode(doc);
var bsonArray = BsonDocument.Parse(json_doc)["catalog"]["book"].AsBsonArray;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.