Jay Kamble

Jay Kamble

  • NA
  • 23
  • 3.7k

Reading Xml using Dom Approach and storing it in Dictionary

Mar 12 2017 5:35 AM
I have this Following XML File:
<Depts>
<Dept Name="Admin">
<Emps>
<Emp>1</Emp>
<Emp>8</Emp>
</Emps>
</Dept>
<Dept Name="HR">
<Emps>
<Emp>3</Emp>
</Emps>
</Dept>
<Dept Name="Operation">
<Emps>
<Emp>5</Emp>
<Emp>9</Emp>
</Emps>
</Dept>
</Depts>
 
 
And I have written this program using DOM to read and store the data into dictionary. But, while printing I am not getting any output. The console remains Blank.
 
The code is below:
 
Dictionary<string, int> dictionary = new Dictionary<string, int>();
XmlDocument document = new XmlDocument();
document.Load("Employee.xml");
XmlNodeList node1 = document.GetElementsByTagName("Dept Name");
foreach (var item in node1)
{
XmlNodeList node2 = document.GetElementsByTagName("//Emps/Emp");
foreach (var item1 in node2)
{ dictionary.Add(item.ToString(),Convert.ToInt32(item1.ToString()));
}
}
foreach (var item in dictionary)
{
Console.WriteLine("Key= " + item.Key + "\nValues= " + item.Value );
}
Console.ReadLine();

Answers (4)