I have created an XML file in the following format:
<?xml version="1.0" encoding="utf-8" ?>
<Employee_Info>
<Employee>
<Name> Blah </Name>
<ID> 001 </ID>
<Dept> ISDC </Dept>
</Employee>
<Employee>
<Name> Bleh </Name>
<ID> 002 </ID>
<Dept> COE </Dept>
</Employee>
<Employee>
<Name> Bah </Name>
<ID> 003 </ID>
<Dept> Roll_Out </Dept>
</Employee>
</Employee_Info>
Now this is the code I’m using to display the data:
XmlTextReader reader = new XmlTextReader(Server.MapPath("~/XMLFile.xml"));
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Response.Write("<" + reader.Name + ">");
break;
case XmlNodeType.Text: //Display the text in each element.
Response.Write(reader.Value + "<br />");
break;
case XmlNodeType.EndElement: //Display the end of the element.
Response.Write("</" + reader.Name + ">");
break;
}
}
Now my output is coming out like this:
Blah
001
ISDC
Bleh
002
COE
Bah
003
Roll_Out
How will I display the tags along with the values? That is I want my output in the following format:
Name: Blah
ID: 001
Dept: COE
And what if I add an extra element in the XML file only at one place like an extra email tag in the 3 employee’s info? How will I read that?
Try following code.
Hope that helps. If it does mark it as answer.
-Milind