首页 > 解决方案 > C#总结列表中的多个项目

问题描述

我想我只需要一点帮助,因为我在这里遇到了一个小问题。

这是来自以特殊 xml 格式存储文件的部署服务器的报告。

该文件如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="XXXXX:9801/MDTMonitorData/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Computers</title>
<id>XXXXXXX:9801/MDTMonitorData/Computers/</id>
<updated>2020-02-26T06:49:07Z</updated>
<link rel="self" title="Computers" href="Computers" />
  <entry>
  <id>http://XXXXXXX:9801/MDTMonitorData/Computers(72)</id>
  <title type="text"></title>
  <updated>2020-02-26T07:24:45Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="Computer" href="Computers(72)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ComputerIdentities" type="application/atom+xml;type=feed" title="ComputerIdentities" href="Computers(72)/ComputerIdentities" />
  <category term="MDT_MonitorModel.Computer" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Name>NB-DE-00011</d:Name>
      <d:PercentComplete m:type="Edm.Int16">100</d:PercentComplete>
      <d:Settings m:null="true" />
      <d:Warnings m:type="Edm.Int32">0</d:Warnings>
      <d:Errors m:type="Edm.Int32">0</d:Errors>
      <d:DeploymentStatus m:type="Edm.Int32">3</d:DeploymentStatus>
      <d:StartTime m:type="Edm.DateTime">2020-02-25T07:33:15.797</d:StartTime>
      <d:EndTime m:type="Edm.DateTime">2020-02-25T08:02:26.653</d:EndTime>
      <d:ID m:type="Edm.Int32">72</d:ID>
      <d:UniqueID m:type="Edm.Guid">509c118c-3489-42c0-91ed-bf5695872a28</d:UniqueID>
      <d:CurrentStep m:type="Edm.Int32">91</d:CurrentStep>
      <d:TotalSteps m:type="Edm.Int32">91</d:TotalSteps>
      <d:StepName></d:StepName>
      <d:LastTime m:type="Edm.DateTime">2020-02-25T08:02:26.653</d:LastTime>
      <d:DartIP m:null="true" />
      <d:DartPort m:null="true" />
      <d:DartTicket m:null="true" />
      <d:VMHost m:null="true" />
      <d:VMName m:null="true" />
    </m:properties>
  </content>
</entry>
<entry>
  <id>http://XXXXXXX:9801/MDTMonitorData/Computers(71)</id>
  <title type="text"></title>
  <updated>2020-02-26T07:24:45Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="Computer" href="Computers(71)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ComputerIdentities" type="application/atom+xml;type=feed" title="ComputerIdentities" href="Computers(71)/ComputerIdentities" />
  <category term="MDT_MonitorModel.Computer" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Name>NB-DE-00210</d:Name>
      <d:PercentComplete m:type="Edm.Int16">100</d:PercentComplete>
      <d:Settings m:null="true" />
      <d:Warnings m:type="Edm.Int32">0</d:Warnings>
      <d:Errors m:type="Edm.Int32">0</d:Errors>
      <d:DeploymentStatus m:type="Edm.Int32">3</d:DeploymentStatus>
      <d:StartTime m:type="Edm.DateTime">2020-02-24T09:01:43.483</d:StartTime>
      <d:EndTime m:type="Edm.DateTime">2020-02-24T09:30:54.38</d:EndTime>
      <d:ID m:type="Edm.Int32">71</d:ID>
      <d:UniqueID m:type="Edm.Guid">0b3cff76-b983-4ac4-bc13-cd36c25318db</d:UniqueID>
      <d:CurrentStep m:type="Edm.Int32">91</d:CurrentStep>
      <d:TotalSteps m:type="Edm.Int32">91</d:TotalSteps>
      <d:StepName></d:StepName>
      <d:LastTime m:type="Edm.DateTime">2020-02-24T09:30:54.63</d:LastTime>
      <d:DartIP m:null="true" />
      <d:DartPort m:null="true" />
      <d:DartTicket m:null="true" />
      <d:VMHost m:null="true" />
      <d:VMName m:null="true" />
    </m:properties>
  </content>
</entry>

不幸的是,我还没有找到一种合理的方法将每个条目中的所有属性都放入一个列表中。

到目前为止,我创建了一个具有属性的类,现在我想把这个糟糕的东西放在一个列表中。

计划:假设列表被称为:mdtobject

MDTObject[0]:
Attr: Name | Value: NB-DE..
Attr:PercentComplete | Value 100

所以一个..

标签: c#xmlparsingmdt

解决方案


在 C# 中

var total = mdtobject.Sum(x => x.[插入要在此处求和的列表项的属性])


推荐阅读