使用XML中的LINQ筛选元素节点

本文关键字:元素 节点 筛选 LINQ XML 中的 使用 | 更新日期: 2023-09-27 18:24:39

我正在尝试使用LINQ 获取标记<ActivityDTTM>26/11/2015 05:25:31</ActivityDTTM>的值

 //I am getting the desire nodes here, but how can I extract the value
 var res = XDocument.Load(fileName)
  .Descendants("CNSDocument").Elements()
  .Select(xe => xe.Name.LocalName);
    }

XML

<packet>
<SequenceID></SequenceID>
<CContextInfo>
    <PatientOID>1000000035339</PatientOID>
    <ClinicalDocumentCode>PatientVisit</ClinicalDocumentCode>
    <SessionKey>ecfiosudc1wzeqtwc5k3cp3a#635841108518316124</SessionKey>
    <TransactionCode>TRGMDMT02</TransactionCode>
    <Identifier>CC_CONT</Identifier>
    <ISTransactional>false</ISTransactional>
    <ISPersist>false</ISPersist>
    <UserID>600000190159</UserID>
    <OrganisationID>600000161694</OrganisationID>
    <MessageDateTime>26/11/2015 05:25:35</MessageTime>
    <RecevingDeviceCode>RY7</RecevingDeviceCode>
    <IsReplayed>false</IsReplayed>
    <TrustIdentifier>RY7</TrustIdentifier>
    <SendingOrgID>RY7</SendingOrgID>
    <SendingSystem>CSCLRC</SendingSystem>
    <ReceivingSystem>KIOSK</ReceivingSystem>
    <iXmlVersion>1.0</iXmlVersion>
    <AppStackVersion>2.666.0.0</AppStackVersion>
    <MessageType>MDM_T02</MessageType>
    <ProfileOID>600000000003</ProfileOID>
</CContextInfo>
<TRGMDMT02>
    <CNSDocuments>
        <CNSDocument>
            <EventType>DOC-C</EventType>
            <DocType>CC_LETMULTICNTPAT</DocType>
            <ActivityDTTM>26/11/2015 05:25:31</ActivityDTTM>
            <CreatedDTTM>26/11/2015 05:24:58</CreatedDTTM>
            <OriginatorCPOID>600000190159</OriginatorCPOID>
            <DocumentUUID>8270DAC1-6230-4254-892E-F44954D3DACC</DocumentUUID>
            <DocumentFileName>Multiple conatct letter- Pat - sq MrKOHLI,virat 20151126052458</DocumentFileName>
            <CompletionStatus>CC_CLN_FINALIZE</CompletionStatus>
            <DocumentName>Multiple conatct letter- Pat - sq MrKOHLI,virat 20151126052458</DocumentName>
            <DocumentTemplateName>Multiple conatct letter- Pat - sq</DocumentTemplateName>
            <DocumentStatus>F</DocumentStatus>
            <DocumentOID>900000023052</DocumentOID>
            <PatientOID>1000000035339</PatientOID>
            <EncType />
            <EncounterOID>1000000037823</EncounterOID>
            <DocumentContentType>3</DocumentContentType>
            <DocumentFormatType>CC_HTML</DocumentFormatType>
            <PLCCommunicationProfileOIDs />
            <IsMDMCheck>1</IsMDMCheck>
        </CNSDocument>
        <HeaderDetails>
            <SendingApplication>CSCLRC</SendingApplication>
            <ProcessID>LZO_R2 _4</ProcessID>
            <CountryCode>CC_NEWZEALAND</CountryCode>
            <AcceptAcknowledge>NE</AcceptAcknowledge>
            <ApplicationAcknowledge>NE</ApplicationAcknowledge>
            <RecevingApplication>KIOSK</RecevingApplication>
            <RecevingFacility>RY7</RecevingFacility>
            <UserID />
        </HeaderDetails>
        <MetaData>
            <BuildNumber>2.666.0.0</BuildNumber>
        </MetaData>
    </CNSDocuments>
    <PatientVisitInformation>
        <PatientVisit>
            <PatientClass>CC_CONT</PatientClass>
            <AssignedPatientLocation>
                <PointOfCare>NSP</PointOfCare>
                <ParentLocation>NSP</ParentLocation>
                <Facility>RY7</Facility>
                <WardName>Not Specified</WardName>
            </AssignedPatientLocation>
            <AdmissionType />
            <EpisodeValue>
                <EpisodeID>2015E0001299</EpisodeID>
                <Facility>RY7</Facility>
            </EpisodeValue>
            <HospitalService />
            <AdmitSource />
            <AmbulatoryService />
            <EncounterValue>
                <EncounterID>2015L0009474</EncounterID>
                <EncounterType>CC_LRCENC</EncounterType>
            </EncounterValue>
            <AdmitDTTM>26/11/2015 10:00:00</AdmitDTTM>
            <DischargeDTTM />
            <ExpectedDischargeDTTM />
            <ReferralSource />
            <PatientType />
            <IsReadmission />
        </PatientVisit>
    </PatientVisitInformation>
    <PatientDemoGraphicDetails>
        <PatientDetails>
            <Sex>CC_Male</Sex>
            <Title>CC_MR</Title>
            <Ethnic />
            <Suffix />
            <Surname>sandy</Surname>
            <Forename>s</Forename>
            <Religion />
            <Deceased>0</Deceased>
            <IsTraced>0</IsTraced>
            <BirthDttm>01/01/0001 00:00:00</BirthDttm>
            <DeathDttm>01/01/0001 00:00:00</DeathDttm>
            <SCNNumber />
            <MiddleName />
            <Nationality />
            <PlaceOfBirth />
            <MultipleBirth />
            <MaritalStatus />
        </PatientDetails>
        <PatientStatus>
            <StatusValueCode />
        </PatientStatus>
        <PatientAddress>
            <Address>
                <CityCode />
                <StateCode />
                <PostalCode />
                <AddressType>CC_USUALADD</AddressType>
                <CountryCode>CC_INDIA_RY1</CountryCode>
                <AddressLine4 />
                <AddressLine3 />
                <AddressLine2 />
                <AddressLine1 />
            </Address>
        </PatientAddress>
        <PatientID>
            <IDType>CC_IDTYTEMPID</IDType>
            <Identifier>T000002406</Identifier>
            <AssigningFacility>RY7</AssigningFacility>
            <AssigningAuthority>RY7</AssigningAuthority>
        </PatientID>
        <OrgROLInfo>
            <OrganisationId>RY7</OrganisationId>
            <OrganisationType>CC_TRUST1</OrganisationType>
            <OrganisationGroup>CC_HEALTH</OrganisationGroup>
            <BusinessAddress>
                <StateCode>MERSEYSIDE</StateCode>
                <PostalCode>CH41 5AL</PostalCode>
                <AddressType>CC_BUSADD</AddressType>
                <CountryCode>CC_UNITEDKINGDOM</CountryCode>
                <AddressLine4>BIRKENHEAD</AddressLine4>
                <AddressLine3 />
                <AddressLine2>HAMILTON STREET</AddressLine2>
                <AddressLine1>OLD MARKET HOUSE</AddressLine1>
            </BusinessAddress>
            <BusinessEmail>
                <ContactType>CC_EMAIL</ContactType>
                <RoleTypeCode>Internet</RoleTypeCode>
                <ContactNumber>rmaniyamghat@csc.com</ContactNumber>
            </BusinessEmail>
            <BusinessContact>
                <ContactType>CC_TELEPHONE</ContactType>
                <RoleTypeCode>Telephone</RoleTypeCode>
                <ContactNumber>0258963147</ContactNumber>
            </BusinessContact>
        </OrgROLInfo>
    </PatientDemoGraphicDetails>
</TRGMDMT02>

使用XML中的LINQ筛选元素节点

如果XML文件中CNSDocument上确实存在,则可以使用以下代码获取值:

var res = xDocument.Descendants("CNSDocument").First().Element("ActivityDTTM").Value;

xDocument.Descendants("CNSDocument")返回一个元素集合,您必须使用First()选择一个元素才能将元素放入其中。

.Descendants("CNSDocument").Descendants("ActivityDTTM").First().Value