Why is my XML reader reading all other elements?

I have built a very simple table that displays 4 columns and 4 rows. When the following code is executed, it displays every other element in the XML file. It does not distinguish between the table row. It reads without any problem and I ran the xml checks so that it is not a syntax problem.

public partial class lblXmlOutput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ConformanceLevel = ConformanceLevel.Document;
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;

        XmlReader reader = XmlReader.Create(Server.MapPath("Part2XMLex.xml"), settings);

        string result = "";

        while (reader.Read())
        {
            if (reader.IsStartElement("td"))
                result += reader.ReadElementContentAsString();

            txtOutput.Text = result;
        }
   }
}

      

+3


source to share


1 answer


Since both .Read()

and .ReadElementContentAsString()

(no parameters) move the reader to the next node.

Change the condition while

to:

while (!reader.EOF)

      



Then add:

else reader.Read();

      

+6


source







All Articles