Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2003, 06:41 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML Traversing using Navigator and Iterator classe

hi,

i have a problem here traversing in XML document. i'm trying to use XPathNavigator class, and all i'm trying to do is to print each node's name and its text. can anyone please help me how to do it with Iterator class and navigator class ?

thanx in advance.
-jasim

following is a sample xml file.


<?xml version='1.0'?>
<bookstore>
  <book>
  First
  </book>
  <title>XML 1</title>
  <name>Writer 1</name>
  <price>100</price>

  <book>
  second
  </book>
  <title>XML 2</title>
  <name>Writer 2</name>
  <price>200</price>

  <book>
  third
  </book>
  <title>XML 3</title>
  <name>Writer 3</name>
  <price>300</price>
</bookstore>
 
Old July 18th, 2003, 03:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

A bit rough and ready:

Code:
using System;
using System.Xml;
using System.Xml.XPath;


namespace LearnXml
{
    /// <summary>
    /// Walks through an Xml document and outputs node data.
    /// </summary>
    class XmlDocumentor
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            XPathDocument xpDoc = new XPathDocument(@"E:\scripts\xml\bookstore.xml");
      XPathNavigator xpNav = xpDoc.CreateNavigator();
      xpNav.MoveToRoot();
      xpNav.MoveToFirstChild();
      ProcessElement(xpNav);     
      Console.ReadLine();
        }

    static void ProcessElement(XPathNavigator Element)
    {
      XPathNavigator xpTemp = Element.Clone();
      if (xpTemp.NodeType == XPathNodeType.Element)
      {
        Console.WriteLine("Element named '{0}' found.", xpTemp.Name);
        if (xpTemp.HasChildren)
        {
          xpTemp.MoveToFirstChild();
          do
          {
            if (xpTemp.NodeType == XPathNodeType.Element)
            {
              ProcessElement(xpTemp);
            }
            else if (xpTemp.NodeType == XPathNodeType.Text)
            {
              Console.WriteLine("The XML string for this node is '{0}'", xpTemp.Value); 
            }             
          }
          while (xpTemp.MoveToNext());
        } 
      }
      xpTemp = null;
    }
    }
}
--

Joe




Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Arrays using JavaScript,/JSP Iterator in IE celticbhoy Javascript How-To 3 December 11th, 2007 10:55 AM
Iterator Scope Problem smokey_gun Javascript How-To 1 July 4th, 2007 01:26 AM
iterator..... s Larryz C# 2005 0 May 9th, 2007 04:06 PM
Traversing UP my hierarchy in XPath grant6607 XSLT 1 May 11th, 2005 10:02 AM
XML Navigator trouble fzilz BOOK: ASP.NET Website Programming Problem-Design-Solution 1 March 16th, 2004 12:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.