Mega Code Archive

 
Categories / C# / XML
 

Gets a value indicating whether the current node is an empty element (for example, MyElement)

using System; using System.IO; using System.Xml; public class Sample  {   public static void Main()   {     XmlTextReader reader = null;     try     {        reader = new XmlTextReader("elems.xml");        while (reader.Read()){          if (reader.IsStartElement()){            if (reader.IsEmptyElement)               Console.WriteLine("<{0}/>", reader.Name);            else{                Console.Write("<{0}> ", reader.Name);                reader.Read(); //Read the start tag.                if (reader.IsStartElement())  //Handle nested elements.                  Console.Write("\r\n<{0}>", reader.Name);                Console.WriteLine(reader.ReadString());  //Read the text content of the element.            }          }        }       }       finally       {         if (reader != null)           reader.Close();      }   } }