Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

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

Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

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

ASKER CERTIFIED SOLUTION
Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

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
Yes that is what I want.  Thanks a ton