简体   繁体   中英

Parse data from XML

I am struggling to read out the "ProtectedElements" node of the xml file below. I can read out the information above that node without an issue, but I cant seem to find a way to read out the checksum from that particular node. When i try to read it, the code returns "null".

I have tried to look up the documentation from Microsoft but the examples dont seem to help. I guess it is something to do with the nesting but I cant figure out a solution. I only want to read out the value of the attribute.

Any help appreciated.

        var doc = XDocument.Load(TempFile);

        // get LockInfo element
        var lockInfoElement = doc.Root.Element(ns + "LockInfo");

        // get lockDate 
        var lockDateAttr = lockInfoElement.Attribute("lockDate");
        var lockDateText = lockDateAttr.Value;
        var lockDate = DateTime.Parse(lockDateText);

        // get locked by 
        var lockedByAttr = lockInfoElement.Attribute("lockedBy");
        var lockedByText = lockedByAttr.Value;
        var lockedBy = lockedByText.ToString();

        // get ValidationInfo element
        var validationInfoElement = doc.Root.Element(ns + "ValidationInfo");

        var validationDateAttr = validationInfoElement.Attribute("validationDate");
        var validationDateText = validationDateAttr.Value;
        var validationDate = DateTime.Parse(validationDateText);

        // get ValidationInfo element
        var validationByAttr = validationInfoElement.Attribute("validatedBy");
        var validationByText = validationByAttr.Value;
        var validationBy = validationByText.ToString();

        // get ConfigSeal checksum element
        var configInfoElement = doc.Root.Element(ns + "ConfigurationSeal");
        var configurationSealAttr = configInfoElement.Attribute("checksum");
        var configurationSealChkSum = configurationSealAttr.Value;
        var configurationSealText = configurationSealChkSum.ToString();

        //--------------------------------------------------------------------

        // get Protected elements checksum
        var protectedElements = doc.Root.Element(ns + "ProtectedElements");
        var protectedElementsAttr = protectedElements.Attribute("checksum");
        var protectedElementsChkSum = protectedElementsAttr.Value;
        var protectedElementsText = protectedElementsChkSum.ToString();

XML DATA:

<SafetyConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:abb-robotics-safety-controller-configuration sc_cfg.1.00.01.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" version="1.00.01" xmlns="urn:abb-robotics-safety-controller-configuration">
  <ValidationInfo validationDate="2019-08-29T13:45:16" validatedBy="W01 Admin"/>
  <LockInfo lockDate="2020-02-11T13:54:07" lockedBy="W01 Admin" controllerId="6700-110323"/>
  <ConfigurationSeal checksum="776890E3E367E5171CC3FD52700BE8FA5373422BCC6C4214C0321E6BEC02EA52" creationDate="2019-08-29T13:39:23.2736126+02:00" createdBy="W01 Admin" systemName="00RZ21_ST010_IR001" swVersion="1.02.04">
    <ProtectedElements checksum="20E318119904F2A8506AD3A00035BE2FE562FA772D371DCC43B04A2E4622550F" />
    <SiosCfg version="1.0">
      <Net name="PNFDevice">
        <Device name="PNFDeviceDev" inSizeBits="64" outSizeBits="64" address="">
          <Module name="InputOutputModule" inputSize="8" outputSize="8" sourceAddress="100" destAddress="144" wdTimeout="500" crcLength="3" fParVersion="2" slot="3" silLevel="2" visible="true" readonly="false" />
var protectedElements = configInfoElement.Element(ns + "ProtectedElements");
var protectedElementsAttr = protectedElements.Attribute("checksum");

On the first line we're looking under configInfoElement rather than doc.Root , and in the second line we're looking under protectedElements rather than configInfoElement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM