Need to read XML child values from root

Hi I am looking to read child xml values from a root without having to reselect each value. I wooul like to get the element name and value.

For instance with the XML and Code attached I would like to end up with

      UserName: BobbyBrady
      ImportFolder: C:\ImportImages
      UserWatchFolder: C:\temp\INFO\BobbyBrady
      DATAFolder: C:\temp\INFO\DATA
      Sharee: CindyBrady
      ShareName: CindyBrady
      ShareText: CindyBradyShare Text4


I have been doing a .Select for each element to get the value  nav.Select("/TestConfig/Tests/Test[@TestName='Test4']/UserName");

Any help is appreciated
private static void getAllUsers(String sFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(sFile);
            XmlNode oNode = doc.DocumentElement;
            XmlNodeList oNodeList;
            oNodeList = oNode.SelectNodes("/TestConfig/Tests/Test[@TestName='Test4']");
 
 
<?xml version="1.0" encoding="utf-8"?>
<TestConfig>
  <Tests>
 
    <Test TestName="Test1">
      <UserName>GregBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\INFO\GregBrady</UserWatchFolder>
    </Test>
 
    <Test TestName="Test2">
      <UserName>MarciaBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\INFO\MarciaBrady</UserWatchFolder>
      <DATAFolder></DATAFolder>
      <Sharee></Sharee>
      <ShareName></ShareName>
      <ShareText></ShareText>
    </Test>
 
    <Test TestName="Test3">
      <UserName>JanBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\INFO\JanBrady</UserWatchFolder>
      <DATAFolder>C:\temp\INFO\DATA</DATAFolder>
      <Sharee>PeterBrady</Sharee>
      <ShareName>PeterBradyShare</ShareName>
      <ShareText>PeterBradyShare Text3</ShareText>
    </Test>
 
    <Test TestName="Test4">
      <UserName>BobbyBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\INFO\BobbyBrady</UserWatchFolder>
      <DATAFolder>C:\temp\INFO\DATA</DATAFolder>
      <Sharee>CindyBrady</Sharee>
      <ShareName>CindyBrady</ShareName>
      <ShareText>CindyBradyShare Text4</ShareText>
    </Test>
 
  </Tests>
</TestConfig>

Open in new window

Charles BaldoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jandromedaCommented:
Try this code.
XmlDocument doc = new XmlDocument();
			doc.Load(@"C:\Xml1.xml");
 
			XmlNodeList list = doc.SelectNodes("//Test[@TestName='Test1']");
			XmlNode tempNode = null;
			
			foreach (XmlNode node in list)
			{
				tempNode = node.SelectSingleNode("UserName");
				
				if (tempNode != null)
				{
					Console.WriteLine(tempNode.InnerText);
				}
				
				tempNode = node.SelectSingleNode("ImportFolder");
				
				if (tempNode != null)
				{
					Console.WriteLine(tempNode.InnerText);
				}
			}

Open in new window

0
jandromedaCommented:
Oops! I think that was not what you wanted. Sorry dude. Here you go.
XmlDocument doc = new XmlDocument();
			doc.Load(@"C:\Xml1.xml");
 
			XmlNodeList list = doc.SelectNodes("//Test[@TestName='Test1']");
			XmlNodeList tempNodeList = null;
			
			foreach (XmlNode node in list)
			{
				tempNodeList = node.ChildNodes;
				
				foreach (XmlNode tempNode in tempNodeList)
				{
					Console.WriteLine(string.Concat(tempNode.Name, ": ", tempNode.InnerText));
				}
 
			}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks

Thats not bad and an improvement to what I have, what I am hoping fro is to be able to iterate through the child elements without knowing what they are.

I will accept this and put a more specific post up
0
Charles BaldoSoftware DeveloperAuthor Commented:
Yes that is what I want.  Thanks a ton
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.