Search code examples
c#stringlistxelement

Adding a string to a list only if it is not empty (from XElement)


Sample code:

public string[] GetMeetingPersonnel(DateTime dateMeeting, string strHistoryDatabase)
{
    DateTime dateMonday = dateMeeting.StartOfWeek(DayOfWeek.Monday);

    XDocument docAssignHistory = XDocument.Load(strHistoryDatabase);
    string strWeekNode = "W" + dateMonday.ToString("yyyyMMdd");
    List<string> listNames = new List<string>();

    var result = docAssignHistory.Descendants(strWeekNode);
    if(result != null)
    {
        foreach (var item in result)
        {
            listNames.Add(item.Descendants("Chairman").First().Value);
            listNames.Add(item.Descendants("AuxCounsellor1").First().Value);
            listNames.Add(item.Descendants("AuxCounsellor2").First().Value);
            listNames.Add(item.Descendants("VideoConferenceHost").First().Value);
            listNames.Add(item.Descendants("VideoConferenceCohost").First().Value);
            listNames.Add(item.Descendants("PrayerOpen").First().Value);
            listNames.Add(item.Descendants("PrayerClose").First().Value);
            listNames.Add(item.Descendants("CBSConductor").First().Value);
            listNames.Add(item.Descendants("CBSReader").First().Value);

            var result2 = item.Descendants("Items").First();
            if(result2 != null)
            {
                var result3 = result2.Descendants("Name");
                foreach(var item2 in result3)
                {
                    listNames.Add(item2.Value);
                }
            }
        }
    }
    return listNames.ToArray();
}

Take for example:

listNames.Add(item.Descendants("Chairman").First().Value);

I want to change all of these lines so that the name is only added if it is not empty. I know I can save the value to a string and then test the string value and add it based of the if result. But is there a more conpact way?


Solution

  • Based on the suggestion in the comments about using a local function, I have come up with this:

    public string[] GetMeetingPersonnel(DateTime dateMeeting, string strHistoryDatabase)
    {
        void AddPersonnel(XElement item, string strAssignment, List<string> listNames1)
        {
            string strName = item.Descendants(strAssignment).First().Value;
            if(strName != string.Empty)
            {
                listNames1.Add(strName);
            }
        }
    
        DateTime dateMonday = dateMeeting.StartOfWeek(DayOfWeek.Monday);
    
        XDocument docAssignHistory = XDocument.Load(strHistoryDatabase);
        string strWeekNode = "W" + dateMonday.ToString("yyyyMMdd");
        List<string> listNames = new List<string>();
    
        var result = docAssignHistory.Descendants(strWeekNode);
        if(result != null)
        {
            foreach (var item in result)
            {
                AddPersonnel(item, "Chairman", listNames);
                AddPersonnel(item, "AuxCounsellor1", listNames);
                AddPersonnel(item, "AuxCounsellor2", listNames);
                AddPersonnel(item, "VideoConferenceHost", listNames);
                AddPersonnel(item, "VideoConferenceCohost", listNames);
                AddPersonnel(item, "PrayerOpen", listNames);
                AddPersonnel(item, "PrayerClose", listNames);
                AddPersonnel(item, "CBSConductor", listNames);
                AddPersonnel(item, "CBSReader", listNames);
    
                var result2 = item.Descendants("Items").First();
                if(result2 != null)
                {
                    var result3 = result2.Descendants("Name");
                    foreach(var item2 in result3)
                    {
                        if(item2.Value != string.Empty)
                        {
                            listNames.Add(item2.Value);
                        }
                    }
                }
            }
        }
        return listNames.ToArray();
    }