nsmgr.AddNamespace (XmlConstants.TnsPrefix, Namespace);
nsmgr.AddNamespace (String.Empty, Namespace);
}
+ if (CheckExtendedPropertyExists ())
+ nsmgr.AddNamespace (XmlConstants.MspropPrefix, XmlConstants.MspropNamespace);
if (atts.Count > 0)
schema.UnhandledAttributes = atts.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
attr.Value = locale.Name;
atts.Add (attr);
+ // Extended properties
+ foreach (DictionaryEntry de in ExtendedProperties) {
+ attr = doc.CreateAttribute (XmlConstants.MspropPrefix, de.Key.ToString (), XmlConstants.MspropNamespace);
+ attr.Value = de.Value != null ? de.Value.ToString () : String.Empty;
+ atts.Add (attr);
+ }
+
elem.UnhandledAttributes = atts.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
XmlSchemaComplexType complex = new XmlSchemaComplexType ();
schema.Items.Add (elem);
- AddConstraintsToSchema (elem, constraintPrefix, tables, relations);
+ AddConstraintsToSchema (elem, constraintPrefix, tables, relations, doc);
foreach (string prefix in nsmgr) {
string ns = nsmgr.LookupNamespace (nsmgr.NameTable.Get (prefix));
if (prefix != "xmlns" && prefix != "xml" && ns != null && ns != String.Empty)
}
return schema;
}
-
+
+ private bool CheckExtendedPropertyExists ()
+ {
+ if (ExtendedProperties.Count > 0)
+ return true;
+ foreach (DataTable dt in Tables) {
+ if (dt.ExtendedProperties.Count > 0)
+ return true;
+ foreach (DataColumn col in dt.Columns)
+ if (col.ExtendedProperties.Count > 0)
+ return true;
+ foreach (Constraint c in dt.Constraints)
+ if (c.ExtendedProperties.Count > 0)
+ return true;
+ }
+ foreach (DataRelation rel in Relations)
+ if (rel.ExtendedProperties.Count > 0)
+ return true;
+ return false;
+ }
+
// Add all constraints in all tables to the schema.
- private void AddConstraintsToSchema (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, DataRelationCollection relations)
+ private void AddConstraintsToSchema (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, DataRelationCollection relations, XmlDocument doc)
{
// first add all unique constraints.
- Hashtable uniqueNames = AddUniqueConstraints (elem, constraintPrefix, tables);
+ Hashtable uniqueNames = AddUniqueConstraints (elem, constraintPrefix, tables, doc);
// Add all foriegn key constraints.
- AddForeignKeys (uniqueNames, elem, constraintPrefix, relations);
+ AddForeignKeys (uniqueNames, elem, constraintPrefix, relations, doc);
}
// Add unique constaraints to the schema.
// return hashtable with the names of all XmlSchemaUnique elements we created.
- private Hashtable AddUniqueConstraints (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables)
+ private Hashtable AddUniqueConstraints (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, XmlDocument doc)
{
- XmlDocument doc = new XmlDocument();
Hashtable uniqueNames = new Hashtable();
foreach (DataTable table in tables) {
- foreach (Constraint constaint in table.Constraints) {
+ foreach (Constraint constraint in table.Constraints) {
- if (constaint is UniqueConstraint) {
+ if (constraint is UniqueConstraint) {
ArrayList attrs = new ArrayList ();
XmlAttribute attrib;
- UniqueConstraint uqConst = (UniqueConstraint)constaint;
+ UniqueConstraint uqConst = (UniqueConstraint) constraint;
XmlSchemaUnique uniq = new XmlSchemaUnique ();
// if column of the constraint is hidden do not write the constraint.
uniq.Fields.Add(field);
}
+ AddExtendedPropertyAttributes (uniq, constraint.ExtendedProperties, doc);
+
elem.Constraints.Add (uniq);
uniqueNames.Add (uniq.Name, null);
}
}
// Add the foriegn keys to the schema.
- private void AddForeignKeys (Hashtable uniqueNames, XmlSchemaElement elem, string constraintPrefix, DataRelationCollection relations)
+ private void AddForeignKeys (Hashtable uniqueNames, XmlSchemaElement elem, string constraintPrefix, DataRelationCollection relations, XmlDocument doc)
{
if (relations == null) return;
- XmlDocument doc = new XmlDocument();
foreach (DataRelation rel in relations) {
if (rel.ParentKeyConstraint == null || rel.ChildKeyConstraint == null)
field.XPath = constraintPrefix+column.ColumnName;
keyRef.Fields.Add(field);
}
+
keyRef.UnhandledAttributes = (XmlAttribute[])attrs.ToArray (typeof (XmlAttribute));
+ AddExtendedPropertyAttributes (keyRef, rel.ExtendedProperties, doc);
+
elem.Constraints.Add (keyRef);
}
}
}
colElem.UnhandledAttributes = (XmlAttribute[])xattrs.ToArray(typeof (XmlAttribute));
+ AddExtendedPropertyAttributes (colElem, col.ExtendedProperties, doc);
seq.Items.Add (colElem);
}
schemaToAdd.Namespaces.Add (prefix, col.Namespace);
}
att.SchemaTypeName = MapType (col.DataType);
+ // FIXME: what happens if extended properties are set on attribute columns??
schemaAttributes.Add (att);
}
+ AddExtendedPropertyAttributes (elem, table.ExtendedProperties, doc);
+
return elem;
}
+ private void AddExtendedPropertyAttributes (XmlSchemaAnnotated xsobj, PropertyCollection props, XmlDocument doc)
+ {
+ ArrayList attList = new ArrayList ();
+ XmlAttribute xmlAttr;
+
+ if (xsobj.UnhandledAttributes != null)
+ attList.AddRange (xsobj.UnhandledAttributes);
+
+ // add extended properties to xs:element
+ foreach (DictionaryEntry de in props) {
+ xmlAttr = doc.CreateAttribute (XmlConstants.MspropPrefix, XmlConvert.EncodeName (de.Key.ToString ()), XmlConstants.MspropNamespace);
+ xmlAttr.Value = de.Value != null ? WriteObjectXml (de.Value) : String.Empty;
+ attList.Add (xmlAttr);
+ }
+ if (attList.Count > 0)
+ xsobj.UnhandledAttributes = attList.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
+ }
+
private string SafeNS (string ns)
{
return ns != null ? ns : String.Empty;