asp.net MVC无法从用于反序列化XML的类中查看属性

本文关键字:属性 XML 反序列化 MVC net 用于 asp | 更新日期: 2023-09-27 18:15:07

我希望反序列化一些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的属性。

谢谢

asp.net MVC无法从用于反序列化XML的类中查看属性

Items是一个项目数组。

修复是在我的执行结束:

应该读:

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);

正如marc指出的,我试图使用一个可枚举的tradeDoublerProducts,而实际上是tradeDoublerProducts。Items是我本应该忽略的可列举的项目。上面的代码运行得很好