C#XML LINQ搜索

时间:2018-03-11 10:22:36

标签: c# xml linq

我有一个像这样的XML文件:

  <SoftwareComponent schemaVersion="1.0" packageID="Y75WC" releaseID="Y75WC" hashMD5="a190fdfa292276288df38507ea551a3b" path="FOLDER04650736M/1/OptiPlex_3050_1.7.9.exe" dateTime="2017-12-05T05:34:30+00:00" releaseDate="décembre 05, 2017" vendorVersion="1.7.9" dellVersion="1.7.9" packageType="LWXP" identifier="532f5a9e-c087-4499-b40c-cf7921ee06d3" rebootRequired="true">
<Name>
  <Display lang="en"><![CDATA[Dell OptiPlex 3050 System BIOS,1.7.9]]></Display>
</Name>
<ComponentType value="BIOS">
  <Display lang="en"><![CDATA[BIOS]]></Display>
</ComponentType>
<Description>
  <Display lang="en"><![CDATA[This package provides the Dell System BIOS update and is supported on Dell OptiPlex 3050 Tower, OptiPlex 3050 Small Form Factor and OptiPlex 3050 Micro for Windows Operation System.]]></Display>
</Description>
<LUCategory value="NONE">
  <Display lang="en"><![CDATA[None]]></Display>
</LUCategory>
<Category value="BI">
  <Display lang="en"><![CDATA[FlashBIOS Updates]]></Display>
</Category>
<SupportedDevices>
  <Device componentID="159" embedded="0">
    <Display lang="en"><![CDATA[OptiPlex 3050 System BIOS]]></Display>
  </Device>
</SupportedDevices>
<SupportedSystems>
  <Brand key="1" prefix="OP">
    <Display lang="en"><![CDATA[Optiplex]]></Display>
    <Model systemID="07A3">
      <Display lang="en"><![CDATA[3050]]></Display>
    </Model>
  </Brand>
</SupportedSystems>
<ImportantInfo URL="http://www.dell.com/support/home/us/en/19/Drivers/DriversDetails?driverId=Y75WC" />
<Criticality value="2">
  <Display lang="en"><![CDATA[Urgent-Dell highly recommends applying this update as soon as possible. The update contains changes to improve the reliability and availability of your Dell system.]]></Display>
</Criticality>

里面有多个SoftwareComponent元素。

我试图根据后代元素(ComponentType值,SupportedSystems-&gt; Device-&gt;显示值,Criticality值)获得SoftwareComponent(dellVersion,hashMD5)的一些属性,但我的所有测试都不好。

查看我的实际代码,我只能获取XML文件中的所有值:

  XDocument doc = XDocument.Load("catalog.xml");
        var els = from el in doc.Root.Elements("SoftwareComponent")
                      select new
                      {
                          dellVersion = (string)el.Attribute("dellVersion"),
                          hashMD5 = (string)el.Attribute("hashMD5"),
                          path = (string)el.Attribute("path"),

                      };

        foreach (var el in els)
        {
            Console.WriteLine("dell BIOS: {0}, MD5: {1}, path: {2}", el.dellVersion, el.hashMD5, el.path);
        }

有人可以告诉我如何继续吗?

由于

1 个答案:

答案 0 :(得分:1)

首先,您的XML文档缺少</SoftwareComponent>结束标记。也许你没有在这里复制内容。

然后,SoftwareComponent实际上是文档中的根,因此您需要以下代码:

XDocument doc = XDocument.Load("catalog.xml");

var el = new
{
    dellVersion = (string)doc.Root.Attribute("dellVersion"),
    hashMD5 = (string)doc.Root.Attribute("hashMD5"),
    path = (string)doc.Root.Attribute("path"),
};

Console.WriteLine("dell BIOS: {0}, MD5: {1}, path: {2}", el.dellVersion, el.hashMD5, el.path);

如果XML具有以下格式,您的代码将正常工作:

<Root>
    <SoftwareComponent schemaVersion="1.0" packageID="Y75WC" releaseID="Y75WC" hashMD5="a190fdfa292276288df38507ea551a3b" path="FOLDER04650736M/1/OptiPlex_3050_1.7.9.exe" dateTime="2017-12-05T05:34:30+00:00" releaseDate="décembre 05, 2017" vendorVersion="1.7.9" dellVersion="1.7.9" packageType="LWXP" identifier="532f5a9e-c087-4499-b40c-cf7921ee06d3" rebootRequired="true">
    </SoftwareComponent>
</Root>

XML文档只能有一个根节点,所以你不能像你想象的那样拥有多个SoftwareComponent

如果你想获得,例如,ComponentType值,你可以这样做:

componentTypeValue = (string)el.Descendants("ComponentType").FirstOrDefault().Attribute("value")

我实际上会将查询更改为foreach,并将FirstOrDefault结果检查为null。