I am hoping to DE-serialize some XML, so I have used XSD to convert the xml into a class.
using System.Xml.Serialization;
namespace Testing.Models
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public class TradeDoublerProducts
{
private TradeDoublersProductsProduct[] itemsField;
[XmlElementAttribute("product", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TradeDoublersProductsProduct[] Items { get; set; }
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
public class TradeDoublersProductsProduct
{
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string productUrl { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string imageUrl { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string description { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string price { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TDProductId { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TDCategoryID { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string sku { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string shortDescription { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string promoText { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string previousPrice { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string shippingCost { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string weight { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string size { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string brand { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string model { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string condition { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string mpn { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string techSpecs { get; set; }
}
}
this all build fine, and looks fine, however I don't think I am doing things right here:
public class ProcessTradeDoubler
{
public void ProcessMyFeed(int feedId)
{
//TODO read XML data in from specified url matching id sent
ProcessXml("data from url");
}
private static void ProcessXml(string myXml)
{
var ser = new XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts tradeDoublerProducts;
using (XmlReader reader = XmlReader.Create(myXml))
{
tradeDoublerProducts = (TradeDoublerProducts)ser.Deserialize(reader);
}
AddModelToProducts((IEnumerable<TradeDoublerProducts>) tradeDoublerProducts);
}
private static void AddModelToProducts(IEnumerable<TradeDoublerProducts> model)
{
// loop through model and add items to database
foreach (var p in model)
{
// this does not work, there is no properties inside Items p.Items.name;
}
}
}
I would expect properties inside my foreach on the model for model.items but I have nothing, please note this is not to test if it reads the XML, or anything to do with the XML file, this is just the pure code and the fact that I should be able to access properties of said XML once i have put into testing.
thanks