locked
Skip start & end element if the row is empty RRS feed

  • Question

  • User-543160537 posted

    Hi,

    How can i skip writer.start and writer.endelement if the row is empty..

    DataTable uniquecpemployees = meetingdetails.AsEnumerable()                            
                                   .GroupBy(r => new { FirstName = r.Field<string>("CS FirstName"), LastName = r.Field<string>("CS LastName") })
                                   .Select(g => g.First())
                                   .CopyToDataTable();
    
    
                                    var UniCptyEmployees = (from DataRow dr in EventDetails.Rows select dr["FirstName"]).Distinct();
    
                                    writer.WriteStartElement("Employees");
    
                                    foreach (var itememp in UniCptyEmployees)
                                    {
                                        if (uniquecpemployees.Rows.Count > 0)
                                        {
                                            
                                            bool flag = false;
                                            //Check whether the title columns contains values
                                            for (int j = 0; j < uniquecpemployees.Rows.Count; j++)
                                            {
                                                if (!string.IsNullOrEmpty(uniquecpemployees.Rows[j]["CS FirstName"].ToString()))
                                                {
                                                    flag = true;
                                                    break;
                                                }
    
                                            }
    
    
                                            if (!string.IsNullOrEmpty(uniquecpemployees.Rows[0]["CS FirstName"].ToString()))
                                            {
                                                if (flag)
                                                {
                                                   
                                                    for (int k = 0; k < uniquecpemployees.Rows.Count; k++)
                                                    {
    
                                                        writer.WriteStartElement("Employee");
                                                        writer.WriteAttributeString("FirstName", null, uniquecpemployees.Rows[k]["CS FirstName"].ToString());
                                                        writer.WriteAttributeString("LastName", null, uniquecpemployees.Rows[k]["CS LastName"].ToString());
                                                       
    
                                                    }
                                                   
                                                }
                                            }
                                        }
    
                                    }
    
                                    writer.WriteFullEndElement();// end Management Compnay Attendences

    With Data:

    -<Employees>
    
    <Employee LastName="adsa" FirstName="asdsa" Email="adsa.com"/>
    
    </Employees>

    Without data i.e empty row is showing as below but i want to skip if the row is empty.

    <Employees></Employees>

    Please advice

    Thanks

    Friday, October 2, 2015 4:27 PM

Answers

  • User-84896714 posted

    Hi sreekanth,

    How can i skip writer.start and writer.endelement if the row is empty..

    You just need to move writer.WriteStartElement("Employees"); into to branch statement. Code below is for your reference.

    if (uniquecpemployees.Rows.Count > 0)
    {
        writer.WriteStartElement("Employees"); 
    }
    if (uniquecpemployees.Rows.Count > 0)
    {
        writer.WriteFullEndElement();
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 2:44 AM