First of all, please read my comment to the question.
Second,
Table
class can NOT contain the member with the same name as its enclosing type. See:
Compiler Error CS0542 | Microsoft Docs[
^]
Third, you don't need a reference to parent object! As you are using XML data, you can get parent object from XML. You can even search for specific table trough the databases objects. See:
void Main()
{
List<Database> databases = new List<Database>
{
new Database(){Name="A", DataSource="B", Tables = new List<Table>
{
new Table(){Schema = "C", Name = "D"},
new Table(){Schema = "E", Name = "F"}
}},
new Database(){Name="E", DataSource="F", Tables = new List<Table>
{
new Table(){Schema = "C", Name = "D"},
new Table(){Schema = "E", Name = "F"}
}}
};
Table a = new Table(){Schema = "C", Name="D"};
List<Database> ParentsOfA = databases
.Where(x=>x.Tables.Any(y=>y.Name == a.Name && y.Schema == a.Schema))
.ToList();
Console.WriteLine($"Table '{a.Name}' with schema of '{a.Schema}' belongs to:");
foreach(Database db in ParentsOfA)
Console.WriteLine($"'{db.Name}' database with datasource: '{db.DataSource}'");
}
public class Database
{
public string Name {get; set;}
public string DataSource {get; set;}
public List<Table> Tables {get; set;}
}
public class Table
{
public string Schema {get; set;}
public string Name {get; set;}
}
Fourth, you can add
DatabaseName
to
Table
class to get unique identifier of database object. This property should act as foreign key ;)
Final note: use XML serialization/deserialization to write/read xml data into/from xml. See:
Details of XML serialization | Microsoft Docs[
^]
Examples of XML Serialization | Microsoft Docs[
^]