Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# simple xml reader skips every other element

Tags:

c#

xml

xmlreader

Hi all I have attempted to write a simple xml reader but have found that it unintentionally skips every other element in the xml file.

Im guessing I am telling it to move onto the next element twice but I am unsure how whats happening or what the solution is.

any help will be greatly appreciated :)

here is a sample of the code and a sample of the xml file

public LevelLoader(string theLevelFile ,ContentManager theContent)
    {
        XmlTextReader reader = new XmlTextReader(theLevelFile);
        while (reader.Read())
        {               
            if (reader.NodeType == XmlNodeType.Element)
            {
                switch (reader.Name)
                {
                    case "tilerow":
                    {                            
                        currentRow = currentRow + 1;
                        Console.WriteLine("row found");
                        currentCol = 0;
                        break;
                    }

                    case "tilecol":
                    {
                        Console.WriteLine("col found");                                                                                                                         
                        currentTexture = reader.ReadElementContentAsFloat();                          
                        currentCol = currentCol + 1;
                        break;
                    }
                }
            }
        }
    }

sample xml

<tilerow>
<tilecol>1</tilecol><tilecol>2</tilecol><tilecol>3</tilecol><tilecol>4</tilecol><tilecol>5</tilecol><tilecol>6</tilecol><tilecol>7</tilecol><tilecol>8</tilecol><tilecol>9</tilecol><tilecol>10</tilecol>
</tilerow>
like image 458
Iain Avatar asked Feb 16 '26 03:02

Iain


2 Answers

The Read() method will first return the Column element, then the Column "Text" then the EndElement. When you use ReadElementContentAsFloat, it reads the current content, then positions the next Read() to the next "Text" section. Since your loop is skipping the Text and the EndElement, it misses 2, 4, ...

Try this instead...

case "tilecol": 
{ 
Console.WriteLine("col found");
reader.Read();
float.TryParse(reader.Value, out currentTexture);
currentCol = currentCol + 1;
break;
} 
like image 102
Les Avatar answered Feb 17 '26 15:02

Les


See this page: http://msdn.microsoft.com/en-us/library/ms223980.aspx

In short, ReadElementContentAsFloat move the reader to the next element and so is the "main" reader.Read() of the loop.

Here is new loop taking into account this new behavior:

while (!reader.EOF)
{
    bool needToRead = true;
    if (reader.NodeType == XmlNodeType.Element)
    {
        switch (reader.Name)
        {
            case "tilerow":
                currentRow = currentRow + 1;
                Console.WriteLine("row found");
                currentCol = 0;
                break;

            case "tilecol":
                Console.WriteLine("col found");                                                                                                                         
                currentTexture = reader.ReadElementContentAsFloat();                          
                currentCol = currentCol + 1;
                needToRead = false;
                break;
        }
    }
    if (needToRead)
        reader.Read();
}