Strongly typed dataset to XML file problems

  • Thread starter Thread starter Walt Borders
  • Start date Start date
W

Walt Borders

Hi

I am new to xml and handling data in xml format.

I am able to read an XML file and schema into a DataGrid.

I can display the columns and values correctly using the
DataGridColumnStyle.

The problem is that when I write the dataSet to a file the nested
relation data does not nest correctly.

I've tried setting the relationship explicitly on the implicit
"tablename_id" column, but I receive an error telling me that a
relationship already exists.

I've dropped the relationship and added the new and receive a run-time
error: "A column name 'Rule' already belongs to this DataTable: cannot
set a nested table name to the same name.

There is a child table named "Rule". It is the child of a table name
"RuleList". There is no column named "Rule" in the DataSet.

At MSDN, and reading the "Nested DataRelations" in particular, it
seems that all I have to do is set NESTED = TRUE. Easier read than
done.

Any help/examples/correction will be very helpful.

Walt Borders



Code Snippets.

myDataSet = new DataSet();

myDataSet.ReadXmlSchema("ConfigSystem.xsd");

myDataSet.ReadXml("ConfigSystem.xml", XmlReadMode.ReadSchema);

this.dataGrid1.SetDataBinding(this.myDataSet,"Rule");


/// Display dataGrid1

/// Push button to Write the file

this.myDataSet.AcceptChanges();

// Re-Write the original file
string filename = "ConfigSystem.xml";

// Create the FileStream to write with.
System.IO.FileStream myFileStream = new System.IO.FileStream(filename,
System.IO.FileMode.Create);

// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter myXmlWriter = new
System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
myXmlWriter.Formatting = Formatting.Indented;
myXmlWriter.Indentation = 3;

// Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter, XmlWriteMode.WriteSchema);

/// Done
 
Wal

There are two things I would verify her

Firstly, load the XML into an XmlDocument and write out its structure to a file, check to see if the XML is as expected

Secondly, check the MappingType for the columns in the DataSet, these can be set to be elements, attributes etc...
 
Thanks Ian,

With success! I used your suggestion of loading the XML into an
XmlDocument. I also bound the dataGrid to the Parent of the 1:N implied
relationship.

For the record:

myDataSet = new DataSet();

myDataSet.ReadXmlSchema("ConfigSystem.xsd");

XmlDataDocument myXmlDocument = new XmlDataDocument(myDataSet);

myXmlDocument.Load("ConfigSystem.xml");

// Bind to the Parent (RuleList) of the Child (Rule):
// In the schema there is an implied 1:N relationship

this.dgRules.SetDataBinding(this.myDataSet,"RuleList");


/// Display dataGrid1

/// Push button to Write the file

this.myDataSet.AcceptChanges();

// Re-Write the original file
string filename = "ConfigSystem.xml";

this.myDataSet.WriteXml("ConfigSystem.xml", XmlWriteMode.WriteSchema);


Schema snippet:

<xs:element name="RuleList" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Rule" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Text" type="xs:string"/>
<xs:element name="alue" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
 
Back
Top