Muhammad Ummar
Muhammad Ummar

Reputation: 3631

XMLReader in silverlight <test /> type tag problem

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.

Upvotes: 0

Views: 1220

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038840

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.

Upvotes: 1

Related Questions