Я надеюсь DE-сериализовать некоторый XML, поэтому я использовал XSD для преобразования XML в класс.
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; }
}
}
все это прекрасно работает и выглядит хорошо, однако я не думаю, что делаю что-то здесь:
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;
}
}
}
Я ожидаю, что свойства внутри моего foreach на модели для model.items
но у меня ничего нет, обратите внимание, это не для проверки, читает ли он XML, или что-то связанное с файлом XML, это просто чистый код и тот факт, что я должен иметь возможность доступа к свойствам указанного XML, как только у меня есть введен в тестирование.
спасибо