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

C# asp.net MVC无法查看用于反序列化XML的类的属性,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,我希望对一些XML进行反序列化,因此我使用XSD将XML转换为类 using System.Xml.Serialization; namespace Testing.Models { [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute] [System.Diagnostics.DebuggerStepThroughAttribute] [S

我希望对一些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;  
        }

    }

}
公共类ProcessTradeDoubler
{
公共void ProcessMyFeed(int feedId)
{
//TODO从发送的指定url匹配id中读取XML数据
ProcessXml(“来自url的数据”);
}
私有静态void ProcessXml(字符串myXml)
{
var ser=新的XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts TradeDoublerProducts;
使用(XmlReader=XmlReader.Create(myXml))
{
tradeDoublerProducts=(tradeDoublerProducts)序列反序列化(读卡器);
}
AddModelToProducts((IEnumerable)tradeDoublerProducts);
}
私有静态void AddModelToProducts(IEnumerable模型)
{
//循环浏览模型并将项添加到数据库
foreach(模型中的var p)
{
//这不起作用,项目p.Items.name中没有属性;
}
}
}
我希望model.items的模型上的foreach中包含属性 但是我什么都没有,请注意,这不是为了测试它是否读取XML,或者是否与XML文件有关,这只是纯粹的代码,并且事实上,一旦我投入测试,我应该能够访问所述XML的属性


谢谢

项目是一组项目。对于每个项目,您应该找到每个项目的属性。

项目是一个项目数组。查看它们,您应该可以找到每个项目的属性。

修复程序在我的执行端:

应改为:

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);
var ser=new-XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts TradeDoublerProducts;
使用(XmlReader=XmlReader.Create(myXml))
{
tradeDoublerProducts=(tradeDoublerProducts)序列反序列化(读卡器);
}
IEnumerable模型=tradeDoublerProducts.Items;
AddModelToProducts(模型);

正如marc指出的,我试图获取可枚举的tradeDoublerProducts,而实际上tradeDoublerProducts.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);
var ser=new-XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts TradeDoublerProducts;
使用(XmlReader=XmlReader.Create(myXml))
{
tradeDoublerProducts=(tradeDoublerProducts)序列反序列化(读卡器);
}
IEnumerable模型=tradeDoublerProducts.Items;
AddModelToProducts(模型);

正如marc指出的,我试图获取可枚举的tradeDoublerProducts,而实际上tradeDoublerProducts.items是我应该传递的可枚举项。上面的代码非常有用。

接受答案,其他人也有愚蠢的时刻来接受答案,其他人也有愚蠢的时刻