XML node reading
-
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.
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 theTextBox
. 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 anXmlTextRead
, 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]