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

  • Home
  • SEARCH
  • 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 146663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:38:36+00:00 2026-05-11T08:38:36+00:00

When parsing an xml document for its nodes or attributes, if the document is

  • 0

When parsing an xml document for its nodes or attributes, if the document is large, I would have a bunch of ifs and else statements.

Obviously, 100+ ifs does not make up maintainable code in the long run.

Rather than doing this, is there another better way? I read on Hanselman’s blog about a friend of his who had the same situation and wrote loads of ifs/else if and generally poor code. Hanselman provided some snippets of a more maintainable way but the entire code isn’t available so it’s a little hard to understand exactly what (the whole picture) is going on. Life after if, else

I am using .NET 3.5 SO I have the full power of extension methods and LINQ available to me. However, I use .NET 2.0 a work so would also appreciate any solutions in v2.0. 🙂

My code looks very similar to the problem on Hanselman’s site:

if (xmlNode.Attributes[‘a’].Value == ‘abc’ {

}
else if (xmlNode.Attributes[‘b’].Value == ‘xyz’
{
wt = MyEnum.Haze;
}

I could just have a dictionary storing the values I am looking for as keys and perhaps a delegate in the value (or whatever I want to happen on finding a required value), so I could say if (containskey) get delegate and execute it, in pseudocode.

This sort of thing goes on and on. Obviously very naive way of coding. I have the same problem with parsing a text document for values, etc.

Thanks

  • 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. 2026-05-11T08:38:37+00:00Added an answer on May 11, 2026 at 8:38 am

    What you’re doing here is executing a list of tests. For each test, if a predicate is true, execute an action. When a test passes, stop processing the list. Right?

    A couple of people have suggested using a dictionary, but the problem with using a dictionary is that you don’t control the order of the items in it. If you want to perform the tests in a specific order (which, as stated, you do), that’s not going to work. So a list seems like the way to go.

    Here’s a functional way to do this, assuming that the predicates are examining an XmlElement.

    Your tests are instances of a class:

    class Test {     string Predicate { get; set; }     Action Verb { get; set; }      Test(string predicate, Action verb)     {        Predicate = predicate;        Verb = verb;     }      bool Execute(XmlElement e)     {         if (e.SelectSingleNode(Predicate) != null)         {             Verb();             return true;         }         return false;     } } 

    To populate the list of tests:

    List<Test> tests = new List<Test>(); tests.Add(new Test('@foo = 'bar'', Method1)); tests.Add(new Test('@foo = 'baz'', Method2)); tests.Add(new Test('@foo = 'bat'', Method3)); 

    To execute the tests:

    foreach (Test t in tests) {    if (t.Execute()) break; } 

    You’ve eliminated a lot of if/else clutter, but you’ve replaced it with this:

    void Method1() {    ... do something here }  void Method2() {    ... do something else here } 

    If your method naming is good, though, this results in pretty clean code.

    To use .NET 2.0, I think you need to add this to the code:

    public delegate void Action(); 

    because I think that type was defined in 3.0. I could be wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Is your function compiled using C or C++ bindings? You… May 11, 2026 at 3:14 pm
  • added an answer RadDock does what RadWindow does. Only, it takes input from… May 11, 2026 at 3:14 pm
  • added an answer $(function(){ $('#loadingGIF').show(); WaitForFunction(); }); function WaitForFunction() { if (!$.isFunction(FUNCTION_TO_WAIT_ON_HERE)) {… May 11, 2026 at 3:14 pm

Related Questions

I need to know how the performance of different XML tools (parsers, validators, XPath
I'm trying to work out the correct way to handle populating an array with
I'm writing some little applications that parse the source of a few web pages,
I'm looking for a free templating engine to generate simple reports. I want some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.