1

我正在尝试解析这些数据:

    <Product>
    <ProductName>Climate Guard</ProductName>
    <Tag>ClimateGuard</Tag>
    <SupportPage>~/Support/ClimateGuard.aspx</SupportPage>
    <ProductPage>~/Products/ClimateGuard.aspx</ProductPage>
    <ProductCategories>
        <ProductCategory>Climate Guard</ProductCategory>
        <PartNumbers>
            <PartNumber Primary="true">CLIMATE GUARD</PartNumber>
            <PartNumber>CLIMATEGUARD LT</PartNumber>
            <PartNumber>CLIMATE GUARD STARTER KIT</PartNumber>
            <PartNumber>SENSOR MODULE</PartNumber>
            <PartNumber>SWCH INP MODULE</PartNumber>
            <PartNumber>TEMP SENSOR</PartNumber>
            <PartNumber>HUMIDITY SENSOR</PartNumber>
            <PartNumber>DOOR CONTACT</PartNumber>
            <PartNumber>MOTION SENSOR</PartNumber>
            <PartNumber>FLOOD DETECTOR</PartNumber>
            <PartNumber>SMOKE DETECTOR</PartNumber>
            <PartNumber>TILT SENSOR</PartNumber>
            <PartNumber>SENSOR CABLE</PartNumber>
            <PartNumber>PWR INP CABLE</PartNumber>
            <PartNumber>100FT 2-WIRE</PartNumber>
            <PartNumber>RJ25 COUPLER</PartNumber>
        </PartNumbers>
    </ProductCategories>
    <Downloads>
        <Download>
            <Version>1.0.27</Version>
            <Url>~/Files/Downloads/ClimateGuard_Firmware_1_0_27.bin</Url>
            <Comment>Firmware</Comment>
        </Download>
        <Download>
            <Version>1.0.6</Version>
            <Url>~/Files/Downloads/ClimateGuard_BuiltInModule_1_0_6.bin</Url>
            <Comment>Built-in Module</Comment>
        </Download>
        <Download>
            <Version>1.0.2</Version>
            <Url>~/Files/Downloads/ClimateGuard_SensorModule_1_0_2.bin</Url>
            <Comment>Sensor Module</Comment>
        </Download>
        <Download>
            <Version>1.0.0</Version>
            <Url>~/Files/Downloads/ClimateGuard_SwitchInputModule_1_0_0.bin</Url>
            <Comment>Switch Input Module</Comment>
        </Download>
    </Downloads>
</Product>

我正在尝试获取零件编号列表,但是,只有第一个出现:

产品类别 Climate Guard 部件号 Climate Guard

我的零件编号代码有什么问题:

public List<Products> GetProducts()
{
    XElement myElement = XElement.Load(HttpContext.Current.Server.MapPath("~/App_Data/products.xml"));
    var query = from a in myElement.Elements("Product")
                select new Products
                {
                    ProductName = a.Element("ProductName").Value,
                    Tag = a.Element("Tag").Value,
                    SupportPage = a.Element("SupportPage").Value,
                    ProductPage = a.Element("ProductPage").Value,
                    ProductCategories = from b in a.Elements("ProductCategories")
                                        select new ProductCategories
                                        {
                                            ProductCategory = b.Element("ProductCategory").Value,
                                            //PartNumbers = GetPartNumbers(myElement.Elements("Product").Elements("ProductCategories").Elements("PartNumbers").Elements("PartNumber"))
                                            PartNumbers = from c in b.Elements("PartNumbers")
                                                          select new PartNumbers
                                                          {
                                                               PartNumber = c.Element("PartNumber").Value
                                                          }
                                        },
                    Downloads = from bb in a.Elements("Downloads").Elements("Download")
                                select new Downloads
                                {
                                    Comment = bb.Element("Comment").Value,
                                    Url = bb.Element("Url").Value,
                                    Version = bb.Element("Version").Value
                                },
                };

    return query.ToList();
}

所有类型(ProductName、Tag 等)都是字符串。PartNumbers 是一个 IEnumerable。

4

2 回答 2

1

目前PartNumber,您没有获取元素值的集合,而是仅获取其父元素的元素,PartNumbers其中包含第一PartNumber个子元素的值。如果你想要PartNumbers类而不是简单的字符串值列表,那么它应该如下所示:

public class PartNumbers
{
    // list instead of single value
    public List<string> Numbers { get; set; } 
}

它应该这样解析:

PartNumbers = new PartNumbers {
       Numbers = b.Element("PartNumbers").Elements()
                  .Select(c => (string)c).ToList()
       }

顺便说一句,您为什么选择如此奇怪的范围变量名称(b用于ProductCategories元素、a用于产品等)?您也可以使用 simpleList<string>来存储零件编号(无需为此创建类):

PartNumbers = b.Element("PartNumbers").Elements().Select(c => (string)c).ToList()
于 2013-01-29T16:40:13.720 回答
0

您可能忘记了 ToList()forProductCategories和。PartNumbersDownloads

public List<Products> GetProducts()
{
    XElement myElement = XElement.Load(HttpContext.Current.Server.MapPath("~/App_Data/products.xml"));
    var query = from a in myElement.Elements("Product")
                select new Products
                {
                    ProductName = a.Element("ProductName").Value,
                    Tag = a.Element("Tag").Value,
                    SupportPage = a.Element("SupportPage").Value,
                    ProductPage = a.Element("ProductPage").Value,
                    ProductCategories = (from b in a.Elements("ProductCategories")
                                        select new ProductCategories
                                        {
                                            ProductCategory = b.Element("ProductCategory").Value,
                                            //PartNumbers = GetPartNumbers(myElement.Elements("Product").Elements("ProductCategories").Elements("PartNumbers").Elements("PartNumber"))
                                            PartNumbers = (from c in b.Elements("PartNumbers")
                                                          select new PartNumbers
                                                          {
                                                               PartNumber = c.Element("PartNumber").Value
                                                          }).ToList()
                                        }).ToList(),
                    Downloads = (from bb in a.Elements("Downloads").Elements("Download")
                                select new Downloads
                                {
                                    Comment = bb.Element("Comment").Value,
                                    Url = bb.Element("Url").Value,
                                    Version = bb.Element("Version").Value
                                }).ToList(),
                };

    return query.ToList();
}
于 2013-01-29T16:39:04.953 回答