There are always 2 ways to skin a cat with XmlDocument - you can walk
the nodes manually (ParentNode, NextSibling, etc), or you can use XPath.
An example is given below; if that doesn't help, can you please clarify
what you are trying to do? Perhaps post some sample code...
Marc
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlElement el in
doc.SelectNodes("/Movies/Movie/Genres/Genre"))
{
string title =
el.ParentNode.ParentNode.SelectSingleNode("Title").InnerText;
string altTitle =
el.SelectSingleNode("../../Title").InnerText;
Console.WriteLine("{0}: {1}/{2}", el.InnerText, title,
altTitle);
}
Marc,
Thanks for the reply, and thanks for helping a newbie..
Here's my my prior working code:
using System;
using System.Xml;
using System.Text;
using System.IO;
namespace TestProg
{
class Program
{
private static void DisplayHelp()
{
Console.Clear();
Console.WriteLine("Usage: The app requires a Xmlfile that
contains the metadata for each movie.");
Console.WriteLine("");
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile2.xml");
XmlNodeList movieList = doc.GetElementsByTagName("Movie");
foreach (XmlNode node in movieList)
{
XmlElement movieElement = (XmlElement)node;
string title =
movieElement.GetElementsByTagName("Title")[0].InnerText;
string actor =
movieElement.GetElementsByTagName("Actor")[0].InnerText;
string genre =
movieElement.GetElementsByTagName("Genre")[0].InnerText;
Console.WriteLine("{0} is performed by {1} and is in
the genre of {2}\n", title, actor, genre);
}
Console.ReadLine();
}
}
}
And the XML that works with it is
<?xml version="1.0" encoding="utf-8" ?>
<Movies>
<Movie>
<Title>Lord Of The Rings</Title>
<Actor>Orlando Bloom</Actor>
<Genre>Fantasy</Genre>
</Movie>
<Movie>
<Title>Harry Potter</Title>
<Actor>Daniel Radcliffe</Actor>
<Genre>Fantasy</Genre>
</Movie>
</Movies>
Even though it works, i'm thinking that that is an expensive way of
doing it with the multiple GetElementsByTagName. If there's a more
elegant way, i don't mind using it too.
I find that if i start to add additonal actors or genres to the Movies
XML, like the original XML example (and shown here below) my attempts
to code for that goes haywire.
<?xml version="1.0" encoding="utf-8" ?>
<Movies>
<Movie>
<Title>Lord Of The Rings</Title>
<Cast>
<Actor>Orlando Bloom</Actor>
<Actor>Sean Bean</Actor>
</Cast>
<Genres>
<Genre>Fantasy</Genre>
</Genres>
</Movie>
<Movie>
<Title>Harry Potter</Title>
<Cast>
<Actor>Daniel Radcliffe</Actor>
</Cast>
<Genres>
<Genre>Fantasy</Genre>
<Genre>Magic</Genre>
</Genres>
</Movie>
</Movies>
This code works for my expected output, but i think it can be refined/
refactored to something more flexible. There's two obvious same block
of code below that parses the actors/genres. I think i need to make it
a more generic method for any future elementtags that i might add to
the XML.
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile2.xml");
XmlNodeList movieList = doc.GetElementsByTagName("Movie");
foreach (XmlNode node in movieList)
{
XmlElement movieElement = (XmlElement)node;
string title =
movieElement.GetElementsByTagName("Title")[0].InnerText;
string actor = "";
string genre = "";
int actorCount =
movieElement.GetElementsByTagName("Actor").Count;
//section to look for the actors in the movie
if (movieElement.GetElementsByTagName("Actor").Count !
= 1)
{
for (int i = 0; i < actorCount; i++)
{
actor =
movieElement.GetElementsByTagName("Actor")
.InnerText + ", " +
actor;
}
//need a way to take away the last comma too
//newbie way..
actor = actor.Substring(0, actor.Length - 2);
}
else
{
actor = movieElement.GetElementsByTagName("Actor")
[0].InnerText;
}
//section for the genres
int genreCount =
movieElement.GetElementsByTagName("Genre").Count;
//section to look for the genres in the movie
if (movieElement.GetElementsByTagName("Genre").Count !
= 1)
{
for (int i = 0; i < genreCount; i++)
{
genre =
movieElement.GetElementsByTagName("Genre").InnerText + ", " +
genre;
}
//need a way to take away the last comma too
//newbie way..
genre = genre.Substring(0, genre.Length - 2);
}
else
{
genre = movieElement.GetElementsByTagName("Genre")
[0].InnerText;
}
Console.WriteLine("{0} is performed by {1} and is in
the genre of {2}\n", title, actor, genre);
}
Console.ReadLine();
}
Any comments will me much appreciated..
thanks!