1

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

1
  • Accept the answer, other people have dumb moments too Commented Nov 19, 2011 at 19:53

2 Answers 2

1

Items is an array of items. Foreach over them, and you should find the properties of each item.

Sign up to request clarification or add additional context in comments.

Comments

0

the fix was on my execution end:

should read:

var ser = new XmlSerializer(typeof(TradeDoublerProducts));
        TradeDoublerProducts tradeDoublerProducts;
        using (XmlReader reader = XmlReader.Create(myXml))
        {
            tradeDoublerProducts = (TradeDoublerProducts) ser.Deserialize(reader);
        }

        IEnumerable<TradeDoublersProductsProduct> model = tradeDoublerProducts.Items;

        AddModelToProducts(model);

as marc pointed out i was trying to take an enumerable tradeDoublerProducts, when in actual fact the tradeDoublerProducts.items was the enumerable item i should have been passing over. the code above works great.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.