I am using DataSet.ReadXmlSchema() to construct a dataset, and DataSet.ReadXml() to load the data.
I am then binding to the dataset to edit the data. I want to control inserting and deleting of rows in the tables in the dataset, based on the minOccurs and maxOccurs properties defined in my schema.
I have tried loading the schema below into an instance of XmlSchema, e.g. MySchema. Given that minOccurs and maxOccurs default to 1, unless otherwise specified, the LastName and FirstName nodes should occur exactly once. Contacts can occur 0 or 1 times, and ContactName can occur 0 to 10 times.
Once the XmlSchema instance is created, how can I reference the minOccurs and maxOccurs properties for the nodes?
I have found that the XmlSchemaParticle class contains MinOccurs and MaxOccurs properties, but I haven’t been able to figure out how to navigate through the XmlSchema instance to find them.
I’ve tried looking at MySchema.Items, but that doesn’t seem to contain the information.
Is this even the “best” approach? Any suggestions would be greatly appreciated.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Person.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<!--Required Nodes-->
<xs:element name="LastName" type="xs:string"/>
<xs:element name="FirstName" type="xs:string"/>
<!-- Optional Nodes-->
<xs:element name="Contacts" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="ContactName" minOccurs="0" maxOccurs="10">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string"/>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Does anyone know how I can find the MinOccurs/MaxOccurs values from an instance of XmlSchema? I have read the documentation at MSDN, and also tried navigating the class through the Visual Studio debugger, but I can not find the correct properties for these values.
I don’t think that the
DataSetwill respect minOccurs/maxOccurs.In fact,
DataSetdoes not map one-to-one to XML Schema. TheDataSetclass maps to a relational database model. Anything in XML Schema that doesn’t map to that model will be ignored or transformed byDataSet.An example was a case where I had elements of the same complex type as child elements of two different parents. I was able to read that schema into the
DataSet, butDataSetin effect created a copy of my shared complex type, in order to match the Relational model. The XML schema I wrote back out did not match the schema I read in.You should try writing out the schema using
WriteXmlSchemaand compare it to the schema you read in.