2

I have a XML file like this:

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

There are multiple SoftwareComponent Elements inside.

I tried to get some attributes of SoftwareComponent ( dellVersion, hashMD5) based on descendants Elements ( ComponentType value, SupportedSystems->Device->Display value, Criticality value) but all my tests were not good.

See my actual code, I can get all the value in the XML file only:

  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);
        }

Somebody can show me how to proceed please ?

Thanks

2
  • You could use HtmlAgilityPack on that. They have pretty good samples on their website. Commented Mar 11, 2018 at 10:25
  • Replace Elements() with Descendants. You did post enough of your xml be sure but I think I'm correct. Commented Mar 11, 2018 at 12:52

1 Answer 1

1

First of all, your XML document is missing a </SoftwareComponent> end tag. Maybe you didn't copy the contents OK here.

Then, SoftwareComponent is actually the root in your document, so you would need code like:

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

Your code would work OK as-is if the XML would have the format:

<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 documents can only have one root node, so you can't have multiple SoftwareComponent as root as you seem to imply.


If you want to get, for example, ComponentType value, you can do:

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

I would actually change the query into a foreach and check that FirstOrDefault result for null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.