Question

Using XmlTextReader.Read(), how do I determine if what the reader read is an end/closing tag?

Note that I'm not looking for the usage of XmlTextReader.IsEmptyElement. I'm looking to verify whether given the following XML:

<thistag what="nothing">
   <inside color="cyan"/>
</thistag>

Can I determine if the thistag tag I read is an opening tag or a end/closing tag?

My solution so far involved checking for the presence of the what attribute:

if (reader.GetAttribute("what") == null)
{
    // it's an end tag!
}else{
    // it's a start tag!
}

But I understand that this approach isn't so elegant, and that it could fail if attributes aren't required for that specific tag.

Was it helpful?

Solution

I haven't tried this, hope this helps:

XmlTextReader reader = null;

reader = new XmlTextReader(filename);
while (reader.Read()) 
{
  if(reader.NodeType==XmlNodeType.Element) // for opening tag
  {
    //your code

  }
  else if(reader.NodeType==XmlNodeType.EndElement) // for closing tag
  {
    //your code

  }
}

OTHER TIPS

XmlNodeType.Element corresponds to opening nodes. XmlNodeType.EndElement is for closing nodes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top