rustyfancy Posted March 10, 2004 Posted March 10, 2004 Does anyone know how to programatically create nested dataset tables within dataset tables in C#? Here's a general idea of my target structure.... <DataSet> < Building > <Building Name> Hospital < /Building Name > < /Building > < Employees> <Employee ID> 10032 < /Employee ID > <Appointments> <Date> 10/1/2004 < /Date > <With Who> Mr. Smith </ With Who> </Appointments> < /Employees> </DataSet> Basically, I have 3 tables..."Building", "Employees", and "Appointments." I need the "Appoinments" table to be nested within the "Employees" table. Any information will help !!!!!!!! --Matt Quote
cyclonebri Posted March 12, 2004 Posted March 12, 2004 Does anyone know how to programatically create nested dataset tables within dataset tables in C#? Here's a general idea of my target structure.... <DataSet> < Building > <Building Name> Hospital < /Building Name > < /Building > < Employees> <Employee ID> 10032 < /Employee ID > <Appointments> <Date> 10/1/2004 < /Date > <With Who> Mr. Smith </ With Who> </Appointments> < /Employees> </DataSet> Basically, I have 3 tables..."Building", "Employees", and "Appointments." I need the "Appoinments" table to be nested within the "Employees" table. Any information will help !!!!!!!! --Matt Matt, There are a couple of different ways to do what you are trying to do. I made a test project and just put this under the code for clicking a button on the form. I am sure you can take it from here, but I hope this will help you do what you are looking to do. [CS] private void button1_Click(object sender, System.EventArgs e) { int MAXBUILDINGS = 2; int MAXEMPLOYEES = 3; int MAXAPPOINTMENTS = 2; System.Xml.XmlDocument myDocument = new System.Xml.XmlDocument(); System.Xml.XmlElement myRoot; System.Xml.XmlElement myNode; System.Xml.XmlElement myNode2; System.Xml.XmlElement myAppts; System.Xml.XmlElement myAppts2; System.Xml.XmlElement myApptDate; System.Xml.XmlElement myApptWithWho; System.Xml.XmlAttribute myAttrib; myRoot = myDocument.CreateElement("dataset"); myDocument.AppendChild(myRoot); myNode = myDocument.CreateElement("buildinglist"); myRoot.AppendChild(myNode); for(int i=0;i<MAXBUILDINGS+1;i++) { myNode2 = myDocument.CreateElement("building"); myAttrib = myDocument.CreateAttribute("name"); switch (i) { case 0: myAttrib.Value = "HOSPITAL"; break; case 1: myAttrib.Value = "STADIUM"; break; case 2: myAttrib.Value = "CHAPEL"; break; } myNode2.Attributes.Append(myAttrib); myNode.AppendChild(myNode2); } myNode = myDocument.CreateElement("employees"); myRoot.AppendChild(myNode); string tmpID = ""; for(int i=0;i<MAXEMPLOYEES+1;i++) { myNode2 = myDocument.CreateElement("employee_id"); myNode.AppendChild(myNode2); tmpID = "1003" + i.ToString(); myNode2.InnerText = tmpID; myAppts = myDocument.CreateElement("appointments"); myNode2.AppendChild(myAppts); for(int j=0;j<MAXAPPOINTMENTS+1;j++) { myAppts2 = myDocument.CreateElement("appointment"); myAppts.AppendChild(myAppts2); myApptDate = myDocument.CreateElement("date"); myAppts2.AppendChild(myApptDate); myApptDate.InnerText = System.DateTime.Now.ToString(); myApptWithWho = myDocument.CreateElement("withwho"); myAppts2.AppendChild(myApptWithWho); switch (j) { case 0: myApptWithWho.InnerText = "Mr A."; break; case 1: myApptWithWho.InnerText = "Mr B."; break; case 2: myApptWithWho.InnerText = "Mr C."; break; case 3: myApptWithWho.InnerText = "Mr D."; break; case 4: myApptWithWho.InnerText = "Mr E."; break; case 5: myApptWithWho.InnerText = "Mr F."; break; } } } MessageBox.Show(myDocument.OuterXml); } [/CS] Good Luck, Brian Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.