I have to read an XML document and create a list of products, each product has an id and other properties that I'm interested in. The problem is, that if the product has the tag then the object will need to have a list of all the variants and I need to create and initialize the variants for each product that has them (you can only partially initialize them from within the product, but you can find their id in there and later after finishing the whole document you can go over the list and fix whatever's needed). What I wanted to do is to go over the whole file and create the lists, then for each product, if it has variants, find them to add the final data to the product variants list and then delete the variant from the main list.
The problem is in how to read the inner parts of an object, if for example reader.read() found the tag , then I met the product, does it mean that when I will use the reader.read() again I will enter more deeply into the hierarchy?
Every help or even reference to a helpful source will be really appreciated, I didn't find good references for a problem like this.
and this is what I tried, there is a part missing because I don't know what to do here:
public List<Product> ParseXml(string xmlFilePath)
{
List<Product> products = new List<Product>();
using(XmlReader reader = XmlReader.Create(xmlFilePath))
{
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element && reader.Name == "product")
{
Product product = new Product();
// Get product attributes
product.ID = reader.GetAttribute("product-id");
// Initialize variation product IDs list
product.Options = new List<ProductOption>();
// Read product elements
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element)
{
switch(reader.Name)
{
case "short-description":
product.Description = reader.ReadElementContentAsString();
break;
case "long-description":
product.DescriptionLong = reader.ReadElementContentAsString();
break;
case "display-name":
product.Name = reader.ReadElementContentAsString();
break;
case "variations":
reader.Read();
if(reader.NodeType == XmlNodeType.Element && reader.Name == "attributes")
{
//Now I need to go deeper and save and initialize a complex ProductOption class with many properties
}
//string variantAttribute1tId = reader.GetAttribute("attribute-id");
// Parse variant attributes
//while(reader.Read())
while(reader.NodeType == XmlNodeType.EndElement && reader.Name != "product")
{
if(reader.NodeType == XmlNodeType.Element && reader.Name == "variant-attribute")
{
string variantAttributeId = reader.GetAttribute("attribute-id");
string variantAttributeValue = reader.ReadElementContentAsString();
// Process variant attribute
// ...
// Add the variant option to the product
product.VariantOptions.Add(productOption);
}
else if(reader.NodeType == XmlNodeType.EndElement && reader.Name == "variant")
{
break;
}
}
break;
}
}
if(reader.NodeType == XmlNodeType.EndElement && reader.Name == "product")
{
// Add the product to the list
products.Add(product);
break;
}
}
}
}
}
return products;
}
This is a sample of the xml file.
<?xml version="1.0" encoding="utf-8"?>
<catalog catalog-id="master-catalog" xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31">
<header>
<image-settings>
<internal-location base-path="/" />
<view-types>
<view-type>large</view-type>
<view-type>medium</view-type>
<view-type>small</view-type>
<view-type>swatch</view-type>
</view-types>
<variation-attribute-id>f54ProductColor</variation-attribute-id>
<alt-pattern>${productname}</alt-pattern>
<title-pattern>${productname}</title-pattern>
</image-settings>
</header>
<product product-id="999111130-m">
<ean />
<upc />
<unit />
<min-order-quantity>1</min-order-quantity>
<step-quantity>1</step-quantity>
<display-name xml:lang="x-default">Hoodie Frank Sinatra</display-name>
<short-description>Best hoodie ever.</short-description>
<long-description>Very very best hoodie everrrrrrrrrrrrrrrrrrrrrrrrrrr.</long-description>
<brand>lemonMarshmello</brand>
<variations>
<attributes>
<variation-attribute attribute-id="f54ProductColor" variation-attribute-id="f54ProductColor">
<display-name>Color</display-name>
<variation-attribute-values>
<variation-attribute-value value="BLACK">
<display-value>black</display-value>
<description></description>
</variation-attribute-value>
<variation-attribute-value value="BEIGE">
<display-value>beige</display-value>
<description></description>
</variation-attribute-value>
</variation-attribute-values>
</variation-attribute>
<variation-attribute attribute-id="f54ProductSize" variation-attribute-id="f54ProductSize">
<variation-attribute-values>
<variation-attribute-value value="SM">
<display-value>S</display-value>
<description>;S-M</description>
</variation-attribute-value>
<variation-attribute-value value="ML">
<display-value>M</display-value>
<description>;M-L</description>
</variation-attribute-value>
</variation-attribute-values>
</variation-attribute>
</attributes>
<variants>
<variant product-id="999111130SM" default="true" />
<variant product-id="999111130ML" />
<variant product-id="999111130SM" />
<variant product-id="999111130ML" />
</variants>
<variation-groups>
<variation-group product-id="999111130" />
<variation-group product-id="999111131" />
</variation-groups>
</variations>
</product>
<product product-id="999111130SM">
<ean />
<upc />
<unit />
<min-order-quantity>1</min-order-quantity>
<step-quantity>1</step-quantity>
<display-name xml:lang="x-default">Hoodie Frank Sinatra</display-name>
<short-description>Best hoodie ever.</short-description>
<long-description>Very very best hoodie everrrrrrrrrrrrrrrrrrrrrrrrrrr.</long-description>
<custom-attributes>
<custom-attribute attribute-id="f54ProductColor">BLACK</custom-attribute>
<custom-attribute attribute-id="f54ProductSize">SM</custom-attribute>
<custom-attribute attribute-id="f54Department">
<value>woman</value>
</custom-attribute>
<custom-attribute attribute-id="f54FilterProductSize">S</custom-attribute>
<custom-attribute attribute-id="f54searchExcluded" xml:lang="x-default">false</custom-attribute>
<custom-attribute attribute-id="f54ComingSoonText"></custom-attribute>
<custom-attribute attribute-id="f54ComingSoonDate"></custom-attribute>
</custom-attributes>
</product>
</catalog>