davidy13

Below is the XML file used:

<MySet>
  <User>
    <Name>John</Name>
    <Order>
      <OrderType>1</OrderType>
      <OrderName>Barley</OrderName>
    </Order>
    <Order>
      <OrderType>2</OrderType>
      <OrderName>Sugar</OrderName>
    </Order>
  </User>
</MySet>

As you can see the "User" table contains an "Order" Table.  In XML which orders belong to which user is very clear, however each order entry does not have a "Name" key which will link the order to the user in traditional relational database like so:

    <Order>
      <Name>John</Name>
      <OrderType>1</OrderType>
      <OrderName>Barley</OrderName>
    </Order>
The xml file is currently put into the 2 bindingsources like so:

            datasetUser.ReadXml(path);
            string relation = datasetUser.Relations[1].RelationName;
            bindsourceUser.DataSource = datasetUser;
            bindsourceUser.DataMember = "User";
            bindsourceOrder.DataSource = bindsourceUser;
            bindsourceOrder.DataMember = relation;

What I want to do is when I add a new user it automatically adds a few orders under that User name, but I don't want to put the <Name> element under <Order> to keep the XML clean.  Right now I add a new row by handling the bindingsourceUser_ListChanged event.

How would I do this   Thanks.



Re: Windows Forms Data Controls and Databinding How to add new child row to xml set with bindingsource WITHOUT an id key?

ke1

When dataset read nested XML, in case your xml, a Key "User_Id" is automatically added by dataset to each User table and Order table.

Try below code.

DataRow myrow;
DataTable tbl1 = datasetUser.Tables["User"];
DataTable tbl2 = datasetUser.Tables["Order"];

myrow = tbl1.NewRow();
myrow["Name"] = "NewName";
tbl1.Rows.Add(myrow);
int n =(int) myrow["User_Id"];
myrow = tbl2.NewRow();
myrow["OrderType"] = 7;
myrow["OrderName"] = "NewItem";
myrow["User_Id"] = n;
tbl2.Rows.Add(myrow);
datasetUser.WriteXml(path);