简体   繁体   中英

C# XML Linq on multiple tags of same name

I have some projects in an XML file. eg. Multiple projects like the one below in the same file . I want to search all project entries where FluidTypes matches a particular string .

<?xml version="1.0" encoding="utf-8"?>
<data>
  <Project ID="P-2014-000037">
    <Name>FDP001_Bakken</Name>
    <Manager>shell</Manager>
    <Area>NAM</Area>
    <Field>Bakken</Field>
    <Type>Time and Material External</Type>
    <Country>USA</Country>
    <Value>3.5</Value>
    <Geomarket>NAM</Geomarket>
    <FormationTypes>Carbonate</FormationTypes>
    <FormationTypes>Coal</FormationTypes>
    <FormationTypes>Fractures</FormationTypes>
    <FormationTypes>Sandstone</FormationTypes>
    <FluidTypes>Gas Cond</FluidTypes>
    <FluidTypes>Heavy Oil</FluidTypes>
    <DriveMechanisms>Compaction</DriveMechanisms>
    <DriveMechanisms>Aquifer</DriveMechanisms>
    <EORProcesses>CO2</EORProcesses>
    <EORProcesses>CSS</EORProcesses>
  </Project>
</data>

I am using the follwing code to search for Geomarket matches :

   IEnumerable<XElement> values1 = from el1 in root.Elements("Project").
        Where(r => regEx1.IsMatch(r.Element("Geomarket").Value))
                                    select el1;

when I use same for Fluid type (which has multiple elements ):

       IEnumerable<XElement> values1 = from el1 in root.Elements("Project").
            Where(r => regEx1.IsMatch(r.Element("FluidTypes").Value))
                                        select el1;

It only checks for a match with the first element with name Fluid Types and not ALL fluid types elements . As a result only Gas Cond matches this project but Heavy Oil does not.

How to make a query across all Fluid types search ?

Use a Where clause with a nested search:

        var projects = root
            .Elements("Project")
            .Where(el => el.Elements("FluidTypes").Where(el2 => regEx1.IsMatch(el2.Value)).Any());

This returns all elements named "Project" with at least one nested element named "FluidTypes" whose Value matches your regular expression.

Or, use a nested Any() :

        var projects = root
            .Elements("Project")
            .Where(el => el.Elements("FluidTypes").Any(el2 => regEx1.IsMatch(el2.Value)));

Try

IEnumerable<XElement> values1 = from el1 in root.Elements("Project").Elements("FluidTypes")
    .Where(r => regEx1.IsMatch(r.Value))
    Select el1;

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