Why is my XML reader reading every other element?

為{幸葍}努か 提交于 2020-01-11 10:05:48

问题


I've 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 discriminate per table row. It reads through without any problem and I have run xml validators so it not a syntax issue.

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;
        }
   }
}

回答1:


Because both .Read() and .ReadElementContentAsString() (the parameterless overload) move the reader to the next node.

Change your while condition to:

while (!reader.EOF)

Then add:

else reader.Read();


来源:https://stackoverflow.com/questions/10038193/why-is-my-xml-reader-reading-every-other-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!