Reading element name from XML File in ASP.Net using C#

I am having a problem to display my Element name that i read from the following XML file in asp.net using C#.

Here is my XML File;

<?xml version=“1.0” encoding=“utf-8” ?>

<storedprocedures>

&lt;ConstantPeriodsSelect&gt;ConstantPeriodsSelect&lt;/ConstantPeriodsSelect&gt;
&lt;PersonSelect&gt;PersonSelect&lt;/PersonSelect&gt;
&lt;ProductSelect&gt;ProductSelect&lt;/ProductSelect&gt;
&lt;UnitsSelect&gt;UnitsSelect&lt;/UnitsSelect&gt;

</storedprocedures>

Here is my Code;

string filename = “c:\\inetpub\\wwwroot\\sales\\Resources\\Resources.xml”;
XmlTextReader tr= new XmlTextReader(filename);// = new XmlReader(filename);

			while(tr.Read())
			{
				if(tr.NodeType == XmlNodeType.Text)
				{
					Response.Write("Name is: " + tr.Name);

Response.Write("Value is: " + tr.Value);
}
}

“tr.Value” working very fine.
But this code always returning empty string for “tr.Name”. Any suggestion.
Thanx in advance.

A node of type “text()” does not have a name. Text nodes are kind of artificial and does have an explicit counterpart in the xml per se. It’s just a node that covers the text contents of another tag. Actually if that other tag has mixed contents (both text and subtags) interleaved, you will have multiple text nodes:

<tag>sometext<subtag>blabla</subtag>someothertext</tag>

will have an element (name=“tag”) with 3 subnodes:
a text node (value=“sometext”),
an element (name=“subtag”) and
a text node (value=“someothertext”)

Thanx Banjymouse, So what changes in My code should I do to get “tr.Name” working…

string filename = “c:\\inetpub\\wwwroot\\sales\\Resources\\Resources.xml”;
XmlTextReader tr= new XmlTextReader(filename);// = new XmlReader(filename);

while(tr.Read())
{
if(tr.NodeType == XmlNodeType.Text)
{
Response.Write("Name is: " + tr.Name);
Response.Write("Value is: " + tr.Value);
}
}

Thanx,
Nawaz Ijaz

I have googled this thing, and in many articles authors used this way to get the name and the value of their Node. Then whats the problem with my code.

You have several options. I would recommend to abstract yourself away from the text-node issue.

You could match on a XmlNodeType.Element instead, and use the InnerText property instead of the Value property.

But I suspect that you are on your way to parse the Xml document yourself. Depending on your objective there may other better-suited alternatives:

  1. Reading the xml into a document and using xpath to select the node/values. This is typically used when you want to go find specific values that form a subset of the entire document, and you want to assume as little as possible about the rest of the document.

  2. Using xml serialization of custom objects to parse the xml file. This means that you are tightly coupled to the structure of the document, but can very easily access individual nodes.

  3. Reading the xml into a dataset, manipulation the xml from a relational perspective.

Thanx for the suggestions, Here is the code that works fine and return both Name and value of the element.

string filename = Server.MapPath(@“~\Resources\Resources.xml”);
XmlTextReader reader= new XmlTextReader(filename);// = new XmlReader(filename);

		while (reader.Read())
		{
			switch (reader.NodeType)
			{
				case XmlNodeType.Element: // The node is an Element
					 Response.Write("&lt;br&gt; Name= " + reader.Name);
				break;
				
				case XmlNodeType.Text:	
					 Response.Write(" and Value is: " + reader.Value);
				break;
				
				
			}
		}
		Response.End();