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 3489126
In Process

The Archive Base Latest Questions

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

I have just written some code, which as i was writing i thought, this

  • 0

I have just written some code, which as i was writing i thought, this is going to be a nice generic method for searching for a particular node. When i finished i actually realised it was a mess 😀

public String sqlReading(String fileName, String path, String nodeId )
{
    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);

    XmlNodeList names = doc.SelectNodes(path);
    foreach (XmlNode xmlDocSearchTerm in names)
    {
        //if the attribute of the node i start at is the same as where i am now
        if (xmlDocSearchTerm.Attributes.Item(0).Value.ToString().Equals(nodeId))
        {
            //get a list of all of its child nodes
            XmlNodeList childNodes = xmlDocSearchTerm.ChildNodes;

            foreach (XmlNode node in childNodes)
            {
                //if there is a node in here called gui display, go inside
                if (node.Name.Equals("GUIDisplay"))
                {
                    XmlNodeList list = node.ChildNodes;
                    //find the sqlsearchstring tag inside of here
                    foreach (XmlNode finalNode in list)
                    {
                        if (finalNode.Name.Equals("sqlSearchString"))
                        {
                            return node.InnerText;
                        }
                    }
                }
            }
        }
    }
    return "";
}

What i intended to do was based on a path – i would start and check to see if the element had the id i was looking for, if it did then i wanted to get inside there and not stop going until i got to the sqlsearchstring tag which was buried two levels deeper. I have managed that, but the issue here is that now i seem to have almost hardcoded a path to the tag opposed to looping there. How could i change my code to stop me from doing this?

Its from the second foreach where its going wrong imo.

Thanks

  • 1 1 Answer
  • 1 View
  • 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-18T11:21:20+00:00Added an answer on May 18, 2026 at 11:21 am

    Haven’t tested it but I believe something like this would work, by using a xpath. However I’m not sure the name of the attribute, or is it always the first attribute?

    public String sqlReading(String fileName, String path, String nodeId)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(fileName);
    
        XmlNode foundNode = doc.SelectNodes(path).SelectSingleNode("*[@id='" + nodeId + "']/GUIDisplay/sqlSearchString");
        if (foundNode != null)
            return foundNode.InnerText;
        return string.Empty;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method somewhat similar to the one written bellow(this is just a
I have written this short script (which I've stripped away some minor detail for
I have just written a small piece of code and it struck me that
I have a small I have just written which uses the following uri template.
I have written a following code to get just the file name without extension
I'm interested in writing some homebrew code for the Microsoft Kinect console. I have
I have special implementation for my grid. For this I wrote some code in
I'm currently working on some performance critical code, and I have a particular situation
I've written some code which created reports and then exports it to .PDF using
I've recently written some Scala code which processes a String, finding all its sub-strings

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.