Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 783
  • Last Modified:

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

0
Charles Baldo
Asked:
Charles Baldo
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now