views:

229

answers:

1

Hi I am parsing XML in silverlight, in my XML I have one tag is like

<test attribute1="123" />
<test1 attribute2="345">abc text</test1>

I am using XMLReader to parse xml like

    using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{

        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
            //process start tag here
                    break;
                case XmlNodeType.Text:
            //process text here
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:

                    break;
                case XmlNodeType.Comment:

                    break;
                case XmlNodeType.EndElement:
            //process end tag here
                    break;
            }
        }
}

but the problem is that for test tag no EndElement is received? which is making my whole program logic wrong. (for test1 tag all works fine). Please help me out.

+1  A: 

In the XmlNodeType.Element case you could test whether it is an empty element using reader.IsEmptyElement property which means that the element is opened and closed in the same iteration.

Darin Dimitrov
Thanks Darin! you saved my time.
Ummar