Deeply nested search through Document tree I have this code block, where I search for the value of Ordnungsbegriff. I have to search through a tree of my CurrentDocument.

How can I rewrite it to be more dynamic? I don't know at which level I will find my value.

string xy = "";        
try
{

    if (string.IsNullOrEmpty(CurrentDocument.Ordnungsbegriff))
    {
        if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.Ordnungsbegriff))
        {
            if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
            {
                if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
                {
                    if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
                    {
                        if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
                        {
                            if (string.IsNullOrEmpty(CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff))
                            {
                                xy = "Nach 6. Ebene, Suche abgebrochen.";
                            }
                            else
                                xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
                        }
                        else
                            xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
                    }
                    else
                        xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
                }
                else
                    xy = CurrentDocument.ParentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
            }
            else
                xy = CurrentDocument.ParentDocument.ParentDocument.Ordnungsbegriff;
        }
        else
            xy = CurrentDocument.ParentDocument.Ordnungsbegriff;
    }
    else
      xy = CurrentDocument.Ordnungsbegriff;

}
catch (Exception)
{
    xy = "Fehler in  Suche";
}
I find recursion complicated You can use a while loop:

private string SearchDocument(Document doc)
{
    while (doc != null) 
    {
        if (!string.IsNullOrEmpty(doc.Ordnungsbegriff))
        {
            return doc.Ordnungsbegriff;
        }
        doc = doc.ParentDocument;
    }
    return null;
}


Edit: As a further point - it's better not to mix languages in your code: Ordnungsbegriff should be Keyword (according to Google).