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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:17:13+00:00 2026-06-11T15:17:13+00:00

I am trying to search or get the xml elements of one file based

  • 0

I am trying to search or get the xml elements of one file based on a query xml file. This query xml file defines which elements will be searched and retrieve their value. The code below does not find all the elements in the xml file even though the element is there:

Anyone can tell me how can I improve my code, or figure out what is the problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ReadXMLRecursively
{
    class Program
    {
        static void Main(string[] args)
        {

            doc.Load(@"C:\Requests.xml");
            doc2.Load(@"C:XmlLists.xml");

            XmlNodeList nList2 = doc2.SelectNodes("//Root/Element");

                    XmlNodeList nList = doc.SelectNodes("//Root/List");

                    foreach (XmlNode xmlNode in nList)
                    {
                        if (nList2 != null)
                            foreach (XmlNode n in nList2)
                            {
                                if (n.Attributes != null)
                                {
                                    string val = n.Attributes.GetNamedItem("SourceCol").Value;

                                    if (xmlNode[val] != null)
                                    {
                                        //if (n.Attributes != null) Console.WriteLine(n.Attributes.GetNamedItem("SourceCol").Value);
                                        XmlElement xmlElement = xmlNode[val];
                                        if (xmlElement != null) Console.WriteLine(xmlElement.Name);
                                    }
                                    else
                                    {
                                        Console.WriteLine(val + " not found");
                                    }
                                }
                            }
                        Console.WriteLine("------------- end------------------");
                    }
    }
}

XML File 1

<root> 
  <list> 
       <FirstName>Abc</FirstName> 
       <LastName>LT</LastName> 
       <Occupatoin>Eng</Occupation> 
       <BirthDate></BirthDate> 
      ... 
 </list> 
</root> 

XML File 2

<root> 
  <Trainings> 
       <Java>Ab</Java> 
       <NET>b</NET> 
       <SQL>c</SQL> 
       <Powershell>d</Powershell> 
      ... 
 </Trainings> 

Search the above xml files base on this xml file

<root> 
    <Element Name="Firstname /> 
    <Element Name="Lastname" /> 
    <Element Name="Occupation" /> 
    <Element Name="Java" /> 
    <Element Name="Net" /> 
    ... 
</root> 
  • 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-06-11T15:17:15+00:00Added an answer on June 11, 2026 at 3:17 pm

    You wrote:

    XmlNodeList nList = doc.SelectNodes(“//Root/List”);

    In your example there is just one element with the name “list”, and so you will get in nList just one node, with one long string that contains all the children nodes, so you cann’t get them by loop.

    If you want to get the children of “list” any element in different node you need to change it to:
    XmlNodeList nList = doc.SelectNodes("//Root/List/*");

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

Sidebar

Related Questions

I'm trying to get this working: in my XML in EditText I have: android:imeActionLabel=Search
I'm trying to read the following xml file from spotify: http://ws.spotify.com/search/1/track?q=sail+awolnation I want to
I'm trying using NHibernate.Search to get Lucene.NET Score through projections. My domain object implements
i am trying to get a search box to work on both click a
I am trying to get a search web application running and needs to use
I'm trying to get sphinx search to return excerpts of matched documents. I'm using
I am trying to get the 'Search Bar and Display Controller' functionality to work
Trying to get a wildcard search to pick up on any text in org_name
I am trying to get strpos to search string variable $string for the phrase
I am trying to get the value user selected from search prompt and pass

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.