0

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 ?

5
  • Why you do not fit this: IEnumerable<XElement> values1 = root.Elements("Project").Elements("FluidTypes"); Commented Apr 2, 2015 at 19:43
  • I want to search all project entries where FluidTypes matches a particular string For example? Commented Apr 2, 2015 at 19:47
  • For example : Search all projects where Fluid types = "Heavy Oil " ` Regex regEx1 = new Regex("Heavy Oil", RegexOptions.IgnoreCase);` Commented Apr 2, 2015 at 19:49
  • @Prasaanth Neelakandan. Use Regex after .Elements("FluidTypes") Commented Apr 2, 2015 at 19:51
  • That doesn't help my case because if I do root.Elements("Project").Elements("FluidTypes"); then only Fluid Type Column is registered in my IEnumerable<XElement> values1 . I need the entire tuple i.e all columns in my IEnumerable<XElement> values1 . Commented Apr 2, 2015 at 22:09

2 Answers 2

3

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)));
Sign up to request clarification or add additional context in comments.

1 Comment

Almost Identical with my question, I would go with this answer lol.
-2

Try

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

1 Comment

Thanks . That doesn't help my case because if I do root.Elements("Project").Elements("FluidTypes"); then only Fluid Type Column is registered in my IEnumerable<XElement> values1 . I need the entire tuple i.e all columns in my IEnumerable<XElement> values1 . IEnumerable<XElement> values1 = from el1 in root.Elements("Project") .Where(r => regEx1.IsMatch(r.Value)) Select el1; I tried this and it seemed to work in this case. However it looks for a match in all elements and not just Fluid Types.

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.