LINQ - 获取元素中的元素

时间:2017-09-13 01:03:17

标签: c# xml vb.net linq

我想从XML文件中提取信息(参见下面的代码的特定部分):

<AssetsInterface>
  <Assets>
     <Asset>
       <Vehicle>
        <MaintenanceHistories>
          <MaintenanceHistory>
            <RepairDate>2014-07-02</RepairDate>
            <Mileage>30692</Mileage>
            <Dealer></Dealer>
            <Activities>
              <Activity>
                <Code></Code>
                <Description>Filtro de aceite</Description>
                <Amount>0</Amount>
              </Activity>
              <Activity>
                <Code></Code>
                <Description>Revisión 30000</Description>
                <Amount>0</Amount>
              </Activity>
            </Activities>
          </MaintenanceHistory>
          <MaintenanceHistory>
            <RepairDate>2015-03-27</RepairDate>
            <Mileage>63984</Mileage>
            <Dealer></Dealer>
            <Activities>
              <Activity>
                <Code></Code>
                <Description>Filtro de aceite</Description>
                <Amount>0</Amount>
              </Activity>
              <Activity>
                <Code></Code>
                <Description>Revisión 60000</Description>
                <Amount>0</Amount>
              </Activity>
              <Activity>
                <Code></Code>
                <Description>Escobillas</Description>
                <Amount>0</Amount>
              </Activity>
              <Activity>
                <Code></Code>
                <Description>Filtro de particulas</Description>
                <Amount>0</Amount>
              </Activity>
            </Activities>
          </MaintenanceHistory>
        </MaintenanceHistories>
      </Vehicle>
    </Asset>
  </Assets>
</AssetsInterface>

如您所见,它有多个<MaintenanceHistory>,其中包含多个<Activities>。我正在寻找的输出是这样的:

2014-07-02 30692 Filtro de aceite
2014-07-02 30692 Revisión 30000
2015-03-27 63984 Filtro de aceite
2015-03-27 63984 Revisión 60000
2015-03-27 63984 Escobillas
2015-03-27 63984 Filtro de partículas

我提取<RepairDate><Mileage>没有问题,即使我提取<Description>,但我不能立即执行此操作。这是我目前的糟糕代码(在x1000不同的尝试之后......):

For Each desc As XElement In document.Descendants("Activity")
    carRepairDesc = desc.Element("Description")

    For Each options As XElement In document.Descendants("MaintenanceHistory")
        carRepairDate = options.Element("RepairDate").Value
        carRepairKMs = options.Element("Mileage").Value
    Next
    Console.WriteLine(carRepairDate & " " & carRepairKMs & "km " & carRepairDesc)
Next

此代码的输出略有不同:

2015-12-28 96833km Filtro de aceite
2015-12-28 96833km Revisión 30000
2015-12-28 96833km Filtro de aceite
2015-12-28 96833km Revisión 60000
2015-12-28 96833km Escobillas
2015-12-28 96833km Filtro de particulas

我希望你有任何建议!

已编辑:

感谢@Hybridzz,这是VB.Net中的解决方案:

Dim document As XDocument = XDocument.Load("PATH TO XML")
Dim mHistory = From r In document.Descendants("MaintenanceHistory") Select New With {
    Key .RepairDate = r.Element("RepairDate").Value,
    Key .Mileage = r.Element("Mileage").Value,
    Key .ActivityDescs = r.Descendants("Activity").[Select](Function(x) x.Element("Description").Value)
     }

For Each r In mHistory
    For Each activityDesc In r.ActivityDescs
        Console.WriteLine(String.Format("{0} {1} {2} {3}", r.RepairDate, r.Mileage, activityDesc, Environment.NewLine))

    Next
Next

1 个答案:

答案 0 :(得分:3)

这是一个c#版本,但您可以更改为VB .net

  XDocument document = XDocument.Load(@"PATH TO XML");

        var mHistory = from r in document.Descendants("MaintenanceHistory")
                    select new
                    {
                        RepairDate = r.Element("RepairDate").Value,
                        Mileage = r.Element("Mileage").Value,
                        ActivityDescs = r.Descendants("Activity").Select(x => x.Element("Description").Value)
                    };

        foreach (var r in mHistory)
        {
            foreach (var activityDesc in r.ActivityDescs)
            {
                Console.WriteLine(string.Format("{0} {1} {2} {3}", r.RepairDate, r.Mileage, activity,  Environment.NewLine));
            }

        }  

,输出

2014-07-02 30692 Filtro de aceite 
2014-07-02 30692 Revisión 30000 
2015-03-27 63984 Filtro de aceite 
2015-03-27 63984 Revisión 60000 
2015-03-27 63984 Escobillas 
2015-03-27 63984 Filtro de particulas