locked
How to skip Element tags if the data is empty RRS feed

  • Question

  • User-543160537 posted

    Hi,

    I am trying to generate XML from excel data, but in one of the element if there is no data i have to skip the starting and ending element but it is showing up elements if there is no data.

     var UniCptyEmployees = (from DataRow dr in EventDetails.Rows select dr["FirstName"]).Distinct();
    
                                  
                                        writer.WriteStartElement("CounterPartyEmployees");
    
                                        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("CounterPartyEmployee");
                                                            writer.WriteAttributeString("FirstName", null, uniquecpemployees.Rows[k]["CS FirstName"].ToString());
                                                           
                                                            writer.WriteFullEndElement();
    
                                                        }
    
                                                    }
                                                }
                                            }
    
                                        }
    
                                        writer.WriteFullEndElement();// end  Attendences

    If the row is empty it is adding  <CounterPartyEmployees></CounterPartyEmployees> which i need to skip. 

    Can someone please help..

    Thanks

    Monday, October 5, 2015 4:25 PM

Answers

  • User-84896714 posted

    Hi sreekanth,

    You need to check the count of uniquecpemployees before you start write start element.

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

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2015 4:23 AM