xml 使用 c# 解析 (BsonDocument)
xml parsing using c# (BsonDocument)
下面是我的 xml 文件,我想解析 AttributeSets 元素。
想使用 C#
将所有数据存储到 mongoDB 数据库
<AttributeSets>
<ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-GB">
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.5590551155</ns2:Height>
<ns2:Length Units="inches">6.6929133790</ns2:Length>
<ns2:Width Units="inches">4.5275590505</ns2:Width>
<ns2:Weight Units="pounds">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
</ns2:ItemAttributes>
</AttributeSets>
到目前为止,这是我的代码。
foreach (var attribute in attributeSet.Any)
{
string xmlFile = ProductsUtil.FormatXml((System.Xml.XmlElement)attribute);
XElement element = XElement.Parse(xmlFile);
XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01";
IEnumerable<object> attribute_Set = element.Descendants()
foreach(System.Xml.Linq.XElement current in attribute_Set)
{
if(current.Name.LocalName == "Brand"){
Item.BRAND = current.Value;
}
else if (current.Name.LocalName == "PackageDimensions"){
var document = new BsonDocument {
// have no idea how to handle here
}
}
}
}
下面是attribute_Set
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.5590551155</ns2:Height>
<ns2:Length Units="inches">6.6929133790</ns2:Length>
<ns2:Width Units="inches">4.5275590505</ns2:Width>
<ns2:Weight Units="pounds">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
下面是我想要的输出(mongoDB JsonView)
"Brand" : "Ambi Pur"
"PackageDimensions" : {
"Height" : {
"Units" : "inches",
"text" : "2.5590551155"
}
"Length" : {
"Units" : "inches",
"text" : "6.6929133790"
}...
}
任何意见和建议将不胜感激。
我将从创建一些 类 开始,将您的 xml 反序列化为:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class AttributeSets
{
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public ItemAttributes ItemAttributes { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd", IsNullable = false)]
public partial class ItemAttributes
{
public string Brand { get; set; }
public ItemAttributesPackageDimensions PackageDimensions { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensions
{
public ItemAttributesPackageDimensionsHeight Height { get; set; }
public ItemAttributesPackageDimensionsLength Length { get; set; }
public ItemAttributesPackageDimensionsWidth Width { get; set; }
public ItemAttributesPackageDimensionsWeight Weight { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsHeight
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsLength
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsWidth
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsWeight
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
然后我们可以使用下面的代码反序列化它:
var xml = @"<AttributeSets>
<ns2:ItemAttributes xmlns:ns2=""http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"" xml:lang=""en-GB"">
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units=""inches"">2.5590551155</ns2:Height>
<ns2:Length Units=""inches"">6.6929133790</ns2:Length>
<ns2:Width Units=""inches"">4.5275590505</ns2:Width>
<ns2:Weight Units=""pounds"">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
</ns2:ItemAttributes>
</AttributeSets>";
using var sr = new StringReader(xml);
var attributeSet = (AttributeSets)new XmlSerializer(typeof(AttributeSets)).Deserialize(sr);
之后,我们可以像任何其他 C# 对象一样将其插入数据库
var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<ItemAttributes>("attributeSets");
await collection.InsertOneAsync(attributeSet.ItemAttributes);
然后我们将能够在 mongo shell:
中看到它
> use test
switched to db test
> db.attributeSets.find().pretty()
{
"_id" : ObjectId("5e60ca560b8dbc44bf1a869f"),
"Brand" : "Ambi Pur",
"PackageDimensions" : {
"Height" : {
"Units" : "inches",
"Value" : "2.5590551155"
},
"Length" : {
"Units" : "inches",
"Value" : "6.6929133790"
},
"Width" : {
"Units" : "inches",
"Value" : "4.5275590505"
},
"Weight" : {
"Units" : "pounds",
"Value" : "0.2645547144"
}
},
"lang" : "en-GB"
}
>
下面是我的 xml 文件,我想解析 AttributeSets 元素。 想使用 C#
将所有数据存储到 mongoDB 数据库 <AttributeSets>
<ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-GB">
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.5590551155</ns2:Height>
<ns2:Length Units="inches">6.6929133790</ns2:Length>
<ns2:Width Units="inches">4.5275590505</ns2:Width>
<ns2:Weight Units="pounds">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
</ns2:ItemAttributes>
</AttributeSets>
到目前为止,这是我的代码。
foreach (var attribute in attributeSet.Any)
{
string xmlFile = ProductsUtil.FormatXml((System.Xml.XmlElement)attribute);
XElement element = XElement.Parse(xmlFile);
XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01";
IEnumerable<object> attribute_Set = element.Descendants()
foreach(System.Xml.Linq.XElement current in attribute_Set)
{
if(current.Name.LocalName == "Brand"){
Item.BRAND = current.Value;
}
else if (current.Name.LocalName == "PackageDimensions"){
var document = new BsonDocument {
// have no idea how to handle here
}
}
}
}
下面是attribute_Set
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.5590551155</ns2:Height>
<ns2:Length Units="inches">6.6929133790</ns2:Length>
<ns2:Width Units="inches">4.5275590505</ns2:Width>
<ns2:Weight Units="pounds">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
下面是我想要的输出(mongoDB JsonView)
"Brand" : "Ambi Pur"
"PackageDimensions" : {
"Height" : {
"Units" : "inches",
"text" : "2.5590551155"
}
"Length" : {
"Units" : "inches",
"text" : "6.6929133790"
}...
}
任何意见和建议将不胜感激。
我将从创建一些 类 开始,将您的 xml 反序列化为:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class AttributeSets
{
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public ItemAttributes ItemAttributes { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd", IsNullable = false)]
public partial class ItemAttributes
{
public string Brand { get; set; }
public ItemAttributesPackageDimensions PackageDimensions { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensions
{
public ItemAttributesPackageDimensionsHeight Height { get; set; }
public ItemAttributesPackageDimensionsLength Length { get; set; }
public ItemAttributesPackageDimensionsWidth Width { get; set; }
public ItemAttributesPackageDimensionsWeight Weight { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsHeight
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsLength
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsWidth
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd")]
public partial class ItemAttributesPackageDimensionsWeight
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units { get; set; }
[System.Xml.Serialization.XmlTextAttribute()]
public decimal Value { get; set; }
}
然后我们可以使用下面的代码反序列化它:
var xml = @"<AttributeSets>
<ns2:ItemAttributes xmlns:ns2=""http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"" xml:lang=""en-GB"">
<ns2:Brand>Ambi Pur</ns2:Brand>
<ns2:PackageDimensions>
<ns2:Height Units=""inches"">2.5590551155</ns2:Height>
<ns2:Length Units=""inches"">6.6929133790</ns2:Length>
<ns2:Width Units=""inches"">4.5275590505</ns2:Width>
<ns2:Weight Units=""pounds"">0.2645547144</ns2:Weight>
</ns2:PackageDimensions>
</ns2:ItemAttributes>
</AttributeSets>";
using var sr = new StringReader(xml);
var attributeSet = (AttributeSets)new XmlSerializer(typeof(AttributeSets)).Deserialize(sr);
之后,我们可以像任何其他 C# 对象一样将其插入数据库
var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<ItemAttributes>("attributeSets");
await collection.InsertOneAsync(attributeSet.ItemAttributes);
然后我们将能够在 mongo shell:
中看到它> use test
switched to db test
> db.attributeSets.find().pretty()
{
"_id" : ObjectId("5e60ca560b8dbc44bf1a869f"),
"Brand" : "Ambi Pur",
"PackageDimensions" : {
"Height" : {
"Units" : "inches",
"Value" : "2.5590551155"
},
"Length" : {
"Units" : "inches",
"Value" : "6.6929133790"
},
"Width" : {
"Units" : "inches",
"Value" : "4.5275590505"
},
"Weight" : {
"Units" : "pounds",
"Value" : "0.2645547144"
}
},
"lang" : "en-GB"
}
>