Click here to Skip to main content
15,915,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code counts cells inside a table. I can't find out why my code is counting all the cells in the file, instead of checking it table per table in the file. Below is my code:

C#
public void CheckCellCountPerTableRow()
        {
            foreach (var table in _xdoc.Descendants("text").Descendants("table"))
            {
                var constantcellcount = true;
                if (!table.IsEmpty)
                {
                    var cellcount = table.Element("row").Elements("cell").Count();
                    foreach (var row in table.Elements("row"))
                    {
                        var ctr = 0;
                        foreach (var cell in row.Elements("cell"))
                        {
                            try
                            {
                                ctr = ctr + int.Parse(cell.Attribute("cols").Value);
                            }
                            catch (NullReferenceException nullReferenceException)
                            {
                                ctr++;
                            }
                        }
                        if (cellcount != ctr)
                        {
                            constantcellcount = false;
                            break;
                        }
                    }
                }
                if (!constantcellcount)
                {
                    var errorlist = Errorlist;
                    var errorModel = new ErrorModel
                    {
                        LineNumber = ((IXmlLineInfo) table).LineNumber,
                        ErrorMessage = "Unequal number of cell distribution in the table",
                        Text = Regex.Match(Regex.Replace(table.ToString(), @"\r\n", ""), "<row>.*?</row>", RegexOptions.Singleline).Value.Replace(" />", "/>")
                    };
                    errorlist.Add(errorModel);
                }
            }
        }



and my xml is below:
XML
<p>
    <table>
        <row>
            <cell>blah</cell>
            <cell/>
        </row>
        <row>
            <cell>blah</cell>
            <cell>90</cell>
        </row>
        <row>
            <cell cols="2"/>
            <cell/>
        </row>
        <row>
            <cell>blah</cell>
            <cell/>
        </row>
        <row>
            <cell>blah</cell>
            <cell>11</cell>
        </row>
    </table>
</p>
<p>blah blah blah</p>
<p>
    <table>
        <row>
            <cell>blah</cell>
            <cell>90</cell>
            <cell/>
        </row>
        <row>
            <cell>blah</cell>
            <cell>90</cell>
            <cell>90</cell>
        </row>
        <row>
            <cell cols="3"/>
        </row>
        <row>
            <cell>blah</cell>
            <cell>90</cell>
            <cell/>
        </row>
        <row>
            <cell>blah</cell>
            <cell>11</cell>
            <cell>90</cell>
        </row>
    </table>
</p>




The problem is it include the second table instead of counting only the element cell in first table. I used foreach so checking is per open and closing tag of table but this was not happen. can some one correct my code. can't find my error
Posted
Updated 23-Dec-15 20:20pm
v3
Comments
ErwinLulu 26-Dec-15 8:45am    
any answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900