Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3622998
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:19:43+00:00 2026-05-18T23:19:43+00:00

I want to search all XML files which contains a special tag, e.g. ‘field’

  • 0

I want to search all XML files which contains a special tag, e.g. ‘field’ within their contents. How can I achieve this with Directory.GetFiles(...) method in C# ?

string[] filePathsFields = Directory.GetFiles(@"E:\Code\", "*.xml", SearchOption.AllDirectories);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:19:44+00:00Added an answer on May 18, 2026 at 11:19 pm

    Let we have

    string path= "E:\\Code";
    

    then

    IEnumerable<XDocument> q =
        Directory.EnumerateFiles(path, "*.xml") // iterate thru each XML file into the dir
        .Select(x => XDocument.Load(x)) // load each file into memory
        .Where(d => doc.Descendants("field").Count() > 0); // determine tag existence
    

    or

    IEnumerable<XDocument> q = from file in Directory.EnumerateFiles(path, "*.xml")
                               let doc = XDocument.Load(file)
                               where doc.Descendants("field").Count() > 0
                               select doc;
    

    Pre .NET 4.0 solution:

    Just replace

    Directory.EnumerateFiles(dir, "*.xml")
    

    with

    Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories)
    

    Select the resulting node itself:

    IEnumerable<IEnumerable<XElement>> qq = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
                                            let doc = XDocument.Load(file)
                                            let field = doc.Descendants("field")
                                            let ignore = doc.Descendants("ignore")
                                            where field.Count() > 0 && ignore.Count() == 0
                                            select field;
    

    returns nested IEnumerable<> because each document can contain several resulting nodes.


    To select only file names:

    IEnumerable<string> q = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
                            let doc = XDocument.Load(file)
                            where doc.Descendants("field").Count() > 0
                            select file;
    

    or if you don’t want to load data to appropriate XDocument object:

    IEnumerable<string> q = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
                            let data = File.ReadAllText(file)
                            where data.Contains("field")
                            select file;
    

    but the last will return a lot of additional stuff and junk, I guess.


    File and folder exclusion:

    var q = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
            where !new[] { "file1", "file2" }.Contains(file)
            let doc = XDocument.Load(file)
            where doc.Descendants("field").Count() > 0
            select file;
    
    var q = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
            where !new[] { "c:\\foo\\bar", "c:\\blah\\blah" }.Contains(Path.GetDirectoryName(file))
            let doc = XDocument.Load(file)
            where doc.Descendants("field").Count() > 0
            select file;
    

    Attributes count condition:

    var q = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
            let doc = XDocument.Load(file)
            from f  in doc.Descendants("field")
            where f.Attributes("id").Count() > 0 && f.Attributes("name").Count() > 0
            select file;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to search all files in a certain directory for occurrences of statements
I want to know which XML parser in java (if at all) can provide
I'm working on a search feature for my application, I want to search all
I want to search for all elements with class needle in all elements returned
We want to search a file to find all instances of bar that are
I want to search a php file to find all calls to a functions
I got this url /search/renttype-all.place-all.type-all.bedrooms-all.0.0/ I want to get the text after the second
I want to search a series of files based on the files name. Here
I have a document which uses an XML namespace for which I want to
my code is showing all the addresses of shops.xml file. but i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.