Question

I am currently working on a .NET project using Xml project. The beginning of the project is pretty simple. I have to have the form caption, controls, form layout, stored procedures / queries used to access and manipulate data etc. set in a configuration file.

My problem is that my form should be generic enough such that updating the application to handle some other data (still a “one to many relation”) comes down to updating the configuration file.

For example I have 2 datagridviews : one for the parent records, one for the child.The relationship between them is of one to many. I have 2 pairs of such kind: The first pair is: Continents(parent table) and Cities(child table). The second pair: Developers(parent table) and Games(child table). Basically what i want is to use Xml files in order to switch between the tables such that the form will show the Continents and Cities and after the switching it will show the Developers and Games. I am new at XML Files.Yes i have read tutorials but they have only helped me with the basic stuff(like the first part of my project which i have mentioned above). If you could give me some ideas ( not necessarily code) I would much appreciate it.

Part of my Xml file( not sure if it helps) for the first parent-child pair:

<?xml version="1.0" encoding="utf-8" ?>
<Form>
  <connectionString>Data Source=dd-PC;Initial Catalog=exemplu;Integrated      Security=True</connectionString>

  <primaryAdapter>
    <SelectCommand>
      <CommandText>Select id as [ID],nume as [Nume] from Continente</CommandText>
    </SelectCommand>
  </primaryAdapter>

  <secondaryAdapter>
    <SelectCommand>
      <CommandText>select id_tara as [ID],id_continent as [Continent],nume as [Nume] from Tari</CommandText>
    </SelectCommand>
  </secondaryAdapter>

  <Bindings>
    <DataMember>ID</DataMember>
    <DataMember>Continent</DataMember>
    <DataMember>Nume</DataMember>
  </Bindings>
etc.

P.S. i am allowed to ONE xml file.

Thank you, I hope I was clear enough.

Était-ce utile?

La solution

Your XML doesn't show the Cities, as you said this was the first parent-child view of it.

Secondly, each pair should be enclosed in a tag, such as <Table> and probably each pair if they have similar items in a <Parent> and <Child> tags respectfully.

Then if the internal tags are the same throughout, you can use the same class to get the information, making putting it into your form straightforward.

Such as:

Table[] tables = XElement.Load(file)
                        .Elements("Table")
                        .Select(table => new Table(table))
                        .ToArray();

Each Table then would have a Parent and a Child of a class of whatever you want to call it to read its data. They should be the same class if they have the same elements, otherwise you will need two different classes.

A Table would be minimally:

public class Table
{
    public readonly XElement element;

    public Table(XElement element){ this.element = element; }

    public int Id { get { return (int)element.Attribute("Id"); } }

    public MyPair Parent
    { get { return _Parent ?? (_Parent = new MyPair(element.Element("Parent"))); } }
    MyPair _Parent;

    public MyPair Child
    { get { return _Child ?? (_Child = new MyPair(element.Element("Child"))); } }
    MyPair _Child;
}

A MyPair would be something like:

public class MyPair
{
    public readonly XElement element;

    public MyPair(XElement element){ this.element = element; }

    public string[] Adapters
    { 
         get 
         { 
             return _Adapters ?? (_Adapters = element.Descendants("CommandText")
                                                     .Select(ct => (string)ct)
                                                     .ToArray());
         }
    } 
    string[] _Adapters;


    public string[] Members
    { 
         get 
         { 
             return _Members ?? (_Members = element.Descendants("DataMember")
                                                   .Select(dm => (string)dm)
                                                   .ToArray());
         }
    } 
    string[] _Members;  
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top