I would suggest creating an XML Schema file which replicates the relationships and the structural elements of the tables in the Dataset and loading it before reading in any XML. Also, I would avoid creating the Child table and the relationship at runtime, rather let it be filled at runtime.
When you do everything at runtime, you are entirely relying on the framework's analysis of the XML schema (analyzed from the XML itself) and Parent-Child relationships often do not form as expected. My 2p. On Dec 18, 1:46 am, Greg <[email protected]> wrote: > Hello, > > I am try to edit an xml file via DataSet in my C# app, by adding a new > table to the structure with multiple nodes to the structure - but the > table should be a child table of one of the existing Tables in the > structure I have read in. > > In other words: > <ParentTable> > <Data1>FOO</Data1> > </ParentTable> > > Should end up like this: > <ParentTable> > <Data1>FOO</Data1> > <MyChildTable> > <Test>BAR</Test> > </MyChildTable> > </ParentTable> > > I'm having problems getting the table in the correct place when I > write the XML after I change it. I am adding the table/fields/ > records, and setting up the relations, but for some reason when I > process the xml the new table I have added just appends to the end of > the xml file and doesnt appear in the branch where it should. > > Here is the code snippet of what I'm doing: > XmlTextReader xmlreader1 = new XmlTextReader("Test.xml"); > > DataSet ds = new DataSet(); > ds.ReadXml(xmlreader1); > > DataTable newTable = new DataTable(); > newTable.TableName = "MyChildTable"; > newTable.Columns.Add( "NewField" ); > newTable.Columns.Add( "ParentTable_ID",System.Type.GetType > ("System.Int32") ); > ds.Tables.Add( newTable ); > > ds.Tables["MyChildTable"].ParentRelations.Add( "MyChildTable", > ds.Tables["ParentTable"].Columns["ParentTable_ID"], ds.Tables > ["MyChildTable"].Columns["ParentTable_ID"], true); > > DataRow newRow; > newRow = ds.Tables["MyChildTable"].NewRow(); > newRow["Test"] = "Value"; > newRow["ParentTable_ID"] = "1"; > ds.Tables["MyChildTable"].Rows.Add(newRow); > > ds.AcceptChanges(); > > StreamWriter myStreamWriter = new StreamWriter("Out.xml" ); > ds.WriteXml(myStreamWriter); > myStreamWriter.Close(); > > Can anyone see what I am doing incorrectly or have any suggestions? > > Thanks, > Greg > [email protected]
