簡體   English   中英

使用 c# (BsonDocument) 解析 xml

[英]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"
}
>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM