将Hierarchion Linq解析为XML

时间:2013-01-29 16:36:15

标签: c# linq xml-parsing

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

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

我正在尝试获取部件号列表,但是,只显示第一个:

产品类别气候卫士 零件号气候卫士

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

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。

2 个答案:

答案 0 :(得分: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()
       }

BTW为什么你选择这么奇怪的范围变量名称(b元素为ProductCategories,产品为a等)?此外,您可以使用简单的List<string>来存储零件编号(无需为此创建类):

PartNumbers = b.Element("PartNumbers").Elements().Select(c => (string)c).ToList()

答案 1 :(得分:0)

您可能忘记了ToList()ProductCategoriesPartNumbers的{​​{1}}。

Downloads