Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML node reading

XML node reading

Scheduled Pinned Locked Moved C#
xmltutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gmeii
    wrote on last edited by
    #1

    I have an xml file that i'm outputing to a textbox. I have a button that is supposed to go to the next node ach time i click it. I know how to output a whole xml file but, i just want to output each node's content with the click of the button.

    H 1 Reply Last reply
    0
    • G gmeii

      I have an xml file that i'm outputing to a textbox. I have a button that is supposed to go to the next node ach time i click it. I know how to output a whole xml file but, i just want to output each node's content with the click of the button.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You'll have to elaborate. If you're displaying the entire XML text in a TextBox, then how do you "go to the next node"? You're already displaying all nodes in the TextBox. Parsing XML, in general, is very easy and there's many ways to do it, from reading the whole file into memory using the XML Document Object Model (DOM) to fast forward-only parsing (SAX, or SAX-like behavior). For forward-only behavior, use an XmlTextRead, move to the first child node of the document element (root node), and keep moving to the next sibling node:

      using System;
      using System.IO;
      using System.Xml;
       
      class Test
      {
      const string xml = @"<?xml version=""1.0""?>
      <root>
      <child>
      <text1>Hello</text1>
      <text2>World</text2>
      </child>
      <child>
      <text1>Howdy</text1>
      <text2>Earth</text2>
      </child>
      </root>";
       
      static void Main()
      {
      using (StringReader sr = new StringReader(xml))
      {
      XmlTextReader reader = new XmlTextReader(sr);
      while (reader.Read())
      {
      if (reader.Name == "child")
      {
      Console.WriteLine(reader.ReadOuterXml());
      Console.WriteLine("----------");
      }
      }
      }
      }
      }

      You could use an XmlDocument to load the whole document into memory and walk the DOM. Again, it really just depends on your requirements. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups