First I was given a XML document LocationFile.xml
<pre lang="msil"><?xml version="1.0" encoding="UTF-8"?>
<IPLocation>
<Mapping ip="204.162.253.44">Haddows</Mapping>
<Mapping ip="192.168.1.13">Others</Mapping>
<Mapping ip="192.168.90.7">Haddows</Mapping>
<Mapping ip="192.168.134.224">Guindy2</Mapping>
<Mapping ip="192.168.15.17">Vadapalani</Mapping>
<Mapping ip="192.168.128.214">AMB-4</Mapping>
<Mapping ip="10.104.19.102">AMB-4</Mapping>
<Mapping ip="10.100.121.67">GreamsRoad</Mapping>
<Mapping ip="131.222.32.24">GreamsRoad</Mapping>
<Mapping ip="10.108.26.80">OMR</Mapping>
<Mapping ip="10.249.9.45">OMR</Mapping>
<Mapping ip="10.101.210.11">OMR</Mapping>
</IPLocation>
Then I was asked to parse the file and store it in hash table and the output should be like when the IP is entered, the corresponding Location should be displayed.
The code I wrote was:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
namespace ConsoleApplication38
{
class parse
{
static void Main(string[] args)
{
XmlDocument file = new XmlDocument();
Hashtable hash = new Hashtable();
string ip = "";
string location = "";
string st;
Console.Write("Enter the IP address:");
st = Console.ReadLine();
file.Load("C:\\Users\\shaheen.n\\Documents\\LocationFile.xml");
XmlNodeList tree = file.GetElementsByTagName("Mapping");
foreach (XmlNode node in tree)
{
XmlAttributeCollection attributes = node.Attributes;
foreach (XmlAttribute att in attributes)
{
ip = att.ChildNodes[0].Value.ToString();
}
location = node.ChildNodes[0].Value.ToString();
hash.Add(ip, location);
}
if (hash.ContainsKey(st))
{
Console.WriteLine("LOCATION corresponding to the entered IP is : {0}",hash[st]);
}
}
}
}
This worked well. Now my mentor wants me to add another attribute (name) in the xml file. so, now my xml file is:
<?xml version="1.0" encoding="UTF-8" ?>
<IPLocation>
<Mapping ip="204.162.253.44" name="ashok">Haddows</Mapping>
<Mapping ip="192.168.1.13" name="alok">Others</Mapping>
<Mapping ip="192.168.90.7" name ="arvind">Haddows</Mapping>
<Mapping ip="192.168.134.224" name ="anand">Guindy2</Mapping>
<Mapping ip="192.168.15.17" name ="anoj">Vadapalani</Mapping>
<Mapping ip="192.168.128.214" name ="ajith">AMB-4</Mapping>
<Mapping ip="10.104.19.102" name="amal">AMB-4</Mapping>
<Mapping ip="10.100.121.67" name="karthik">GreamsRoad</Mapping>
<Mapping ip="131.222.32.24" name="jayanthi">GreamsRoad</Mapping>
<Mapping ip="10.108.26.80" name="abirami">OMR</Mapping>
<Mapping ip="10.249.9.45" name="anandhi">OMR</Mapping>
<Mapping ip="10.101.210.11" name ="brindha">OMR</Mapping>
</IPLocation>
Now, I should get the the newly added attribute 'name' from the XML file and store it in a string array. I am new to c#..I have no idea what to do..please tell me what should I add in my code..?