Below is my xml file and I want to parse the AttributeSets element. want to store all the data to mongoDB database using C#
<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>
Here is my code so far.
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
}
}
}
}
Below is 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>
Below is my desire output ( mongoDB JsonView)
"Brand" : "Ambi Pur"
"PackageDimensions" : {
"Height" : {
"Units" : "inches",
"text" : "2.5590551155"
}
"Length" : {
"Units" : "inches",
"text" : "6.6929133790"
}...
}
any advice and suggestions will be greatly appreciated.
I'd start off with creating some classes to deserialize your xml in to:
Then we can deserialize it using the code below:
After that, we can just insert it into the database like any other C# object
We'll then be able to see it in the mongo shell: