node iterator question (simple)

  • Thread starter Thread starter suzy
  • Start date Start date
S

suzy

i have some xml like this:

<departments>
<department name="finance">
<employee name="tom hanks" age="23"/>
<employee name="tom selleck" age="43"/>
</department>
<department name="admin">
<employee name="nicole kidman" age="33"/>
<employee name="tom cruise" age="44"/>
<employee name="harrison ford" age="42"/>
<employee name="nicholas cage" age="23"/>
</department>
</departments>


i want to loop through each "department" and then loop through each of its
corresponding "employees" picking of the attribute values. how can i do
this in .net using the xpathnavigator and xpathnodeiterator objects. i have
managed to retrieve the department name, but i am stuck trying to get
information out the its children.

many thanks.
 
Something like below, perhaps?

XPathNavigator navigator = xpdoc.CreateNavigator(); // I assume you have a
navigator already
XPathNodeIterator iterator = navigator.Select("//department"); And I assume
you have an iterator similar to this for the department.

while (iterator.MoveNext())
{
XPathNodeIterator employeeIterator =
iterator.Current.Select("employee");
while (employeeIterator.MoveNext())
{
string empName = employeeIterator.Current.GetAttribute("name", "");
}
}

- Jiho
 
perfect! thank you.

Jiho Han said:
Something like below, perhaps?

XPathNavigator navigator = xpdoc.CreateNavigator(); // I assume you have a
navigator already
XPathNodeIterator iterator = navigator.Select("//department"); And I assume
you have an iterator similar to this for the department.

while (iterator.MoveNext())
{
XPathNodeIterator employeeIterator =
iterator.Current.Select("employee");
while (employeeIterator.MoveNext())
{
string empName = employeeIterator.Current.GetAttribute("name", "");
}
}

- Jiho
 
The following code snippet seems to do what you want:

XmlTextReader reader = new XmlTextReader("XPath.xml");
XPathDocument doc = new XPathDocument(reader);

XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("/departments/department");
while(iter.MoveNext()) {
XPathNavigator sub = iter.Current;
sub.MoveToAttribute("name", string.Empty);
Console.WriteLine("Dept name: " + sub.Value);
sub.MoveToParent();

XPathNodeIterator subIter = sub.Select("./employee");
while(subIter.MoveNext()) {
XPathNavigator sub2 = subIter.Current;
if (sub2.MoveToFirstAttribute()) {
do {
Console.WriteLine("Employee: " + sub2.LocalName + ": " +
sub2.Value);
} while (sub2.MoveToNextAttribute());
}
}
Console.WriteLine();
}

Prints out:

Dept name: finance
Employee: name: tom hanks
Employee: age: 23
Employee: name: tom selleck
Employee: age: 43

Dept name: admin
Employee: name: nicole kidman
Employee: age: 33
Employee: name: tom cruise
Employee: age: 44
Employee: name: harrison ford
Employee: age: 42
Employee: name: nicholas cage
Employee: age: 23

Thanks,
Priya
 
Hope this would help.

using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.XPath;

class TestMain
{
[STAThread]
static void Main(string[] args)
{
if(0 == args.Length)
return;

string inputxmlfile = args[0];

if(File.Exists(inputxmlfile))
{
OpenXmlDoc(inputxmlfile);
}
}

static void OpenXmlDoc(string filename)
{
try
{
XmlReader reader = new XmlTextReader(filename);
XPathDocument doc = new XPathDocument(reader); // read-only.
XPathNavigator nav = doc.CreateNavigator();
GetNode(nav, "departments/department");
}
catch(System.Xml.XPath.XPathException e)
{
Console.WriteLine(e.Message);
}
}

static void GetNode(XPathNavigator nav, string xpath)
{
// Set the node iterator to the return value of the Xpath.
XPathNodeIterator iterator = nav.Select(xpath);
while(iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
// Get parameters.
string name = nav2.GetAttribute("name", "");
Debug.Assert(null != name, "name is null.");
Console.WriteLine("{0} Name {1}", nav2.Name, name);

// navigate thru <employee> nodes of the current navigator.
GetNode(nav2, "employee");
}
}
}
 
Back
Top