using System;
using System.Xml;

namespace InfoReaderLib
{
	/// <summary>
	/// Reads an xml document that contains ficticious 
	/// person information and returns the details.
	/// </summary>
	public class InfoReader
	{
		private string lastNameToLookup;
		private XmlElement personNode;
	
		public InfoReader(string theLastNameToLookup)
		{
			lastNameToLookup = theLastNameToLookup;
		
			XmlDocument document = new XmlDocument();
			document.Load(@"c:\temp\data\info.xml");
		
			XmlNodeList persons = document.GetElementsByTagName("person");

			foreach(XmlNode aNode in persons)
			{
				XmlElement person = (XmlElement) aNode;
				string lastName = person.GetElementsByTagName("last").Item(0).FirstChild.Value;
				if (lastName == lastNameToLookup)
				{
					personNode = aNode as XmlElement;
					break;
				}
			}		
		}
	
		public string getFullName()
		{
			String result = "Name not found";
			if (personNode != null)
			{
				String lastName = personNode.GetElementsByTagName("last").Item(0).FirstChild.Value;
				String firstName = personNode.GetElementsByTagName("first").Item(0).FirstChild.Value;
				result = firstName + " " + lastName;
			}
			return result;
		}
	
		public string getRecord()
		{
			String result = "Name not found";
			if (personNode != null)
			{
				result = personNode.GetElementsByTagName("record").Item(0).FirstChild.Value;
			}
			return result;
		}

		public string getPictureFileName()
		{
			String result = null;
			if (personNode != null)
			{
				result = personNode.GetElementsByTagName("img").Item(0).FirstChild.Value;
			}
			return result;
		}
	}
}
