Hi all,
I am new to .NET and the way XML data is handled in .NET
I wrote a small application with .NET forms in C++
The application reads data from an XML file to fill-in
the fields of the form. This works fine but when I try to add
new data to the XML file it does not comply with the XML schema
file I am using.
Here some xml and code snipplets:
Schema description:
<xs:element name="Store" type="StoreType" />
<xs:complexType name="StoreType">
<xs:sequence maxOccurs="unbounded">
<xs:element name="Config" type="ConfigurationType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ConfigurationType">
<xs:sequence>
... simple elements of type "string" and "long" ...
</xs:sequence>
<xs:attribute name="configName" type="xs:string" />
</xs:complexType>
An xml file made with this schema manually:
<Store xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd
Config.xsd">
<Config configName="TestA">
<FileName>C:/temp/keineAhnungX.mpg</FileName>
...
</Config>
</Store>
After adding a new config element in the .NET application the XML output
looks like this:
<NewDataSet xmlns="http://tempuri.org/XMLSchema.xsd">
<Store>
<Config configName="TestA">
<FileName>C:/temp/keineAhnungX.mpg</FileName>
...
</Config>
</Store>
<Config configName="TestD">
<FileName>TestFile</FileName>
</Config>
</NewDataSet>
I have now a new Element which does not exist in the schema "NewDataSet"
and the added config element is not a sub element of the Store element.
This is very confusing or shall I say disappointing.
<code>
// Here is the way I am dealing with the xml data
// This uses the xml Schema description which is first one in my post
this->xmlData = new XmlDataDocument();
this->xmlData->DataSet->ReadXmlSchema("Config.xsd");
// reading the xsd compliant xml data
XmlTextReader* reader = new XmlTextReader("Daten.xml");
reader->MoveToContent(); // Moves the reader to the root node.
this->xmlData->Load(reader);
// now I get two DataTable, one is the Store table containing all
// store elements
// but I am interested in the other one, the Config table
DataTableCollection* myTableCollection = xmlData->DataSet->Tables;
// I am using the "Config" DataTable to append a new element behind the
// last config element.
DataRow* newData = this->configTable->NewRow();
newData->set_Item(S"configName", S"TestD");
newData->set_Item(S"FileName", S"TestFile");
this->configTable->Rows->Add(newData);
// now I am writing the XML back to disc resulting in an unusable
// non-complying data file
XmlTextWriter* xmlWriter =
new XmlTextWriter("test.xml", System::Text::Encoding::UTF8);
xmlWriter->Formatting = Formatting::Indented;
this->xmlData->Write(xmlWriter);
xmlWriter->Close();
</code>
What is wrong in the way I am using the XML framework of .NET?
TIA
Michael