System.Data.XmlTreeGen.HandleColumn C# (CSharp) Method

HandleColumn() private method

private HandleColumn ( DataColumn col, XmlDocument dc, XmlElement schema, bool fWriteOrdinal ) : XmlElement
col DataColumn
dc XmlDocument
schema XmlElement
fWriteOrdinal bool
return XmlElement
        internal XmlElement HandleColumn(DataColumn col, XmlDocument dc, XmlElement schema, bool fWriteOrdinal)
        {
            XmlElement root;
            int minOccurs;

            Debug.Assert(col.ColumnMapping != MappingType.SimpleContent, "Illegal state");

            string refString = (col.ColumnMapping != MappingType.Element) ? Keywords.XSD_ATTRIBUTE : Keywords.XSD_ELEMENT;
            root = dc.CreateElement(Keywords.XSD_PREFIX, refString, Keywords.XSDNS);

            // First add any attributes.
            root.SetAttribute(Keywords.NAME, col.EncodedColumnName);

            if (col.Namespace.Length == 0)
            {
                DataTable _table = col.Table;
                // We need to travese the hirerarchy to find the targetnamepace
                string tgNamespace = FindTargetNamespace(_table);
                if (col.Namespace != tgNamespace)
                {
                    root.SetAttribute(Keywords.FORM, Keywords.UNQUALIFIED);
                }
            }

            if (col.GetType() != typeof(DataColumn))
                AddXdoProperties(col, root, dc);
            else
                AddColumnProperties(col, root);


            AddExtendedProperties(col._extendedProperties, root);
            HandleColumnType(col, dc, root, schema);
            if (col.ColumnMapping == MappingType.Hidden)
            { // CDT / UDT can not be mapped to Hidden column
                if (!col.AllowDBNull)
                    root.SetAttribute(Keywords.MSD_ALLOWDBNULL, Keywords.MSDNS, Keywords.FALSE);

                if (!col.DefaultValueIsNull)
                    if (col.DataType == typeof(bool))
                        root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, (bool)(col.DefaultValue) ? Keywords.TRUE : Keywords.FALSE);
                    else
                    {
                        XmlTreeGen.ValidateColumnMapping(col.DataType);
                        root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, col.ConvertObjectToXml(col.DefaultValue));
                    }
            }


            if ((!col.DefaultValueIsNull) && (col.ColumnMapping != MappingType.Hidden))
            {
                XmlTreeGen.ValidateColumnMapping(col.DataType);
                if (col.ColumnMapping == MappingType.Attribute && !col.AllowDBNull)
                {
                    if (col.DataType == typeof(bool))
                    {
                        root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, (bool)(col.DefaultValue) ? Keywords.TRUE : Keywords.FALSE);
                    }
                    else
                    { // CDT / UDT columns cn not be mapped to Attribute also
                        root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, col.ConvertObjectToXml(col.DefaultValue));
                    }
                }
                else
                { // Element Column : need to handle CDT
                    if (col.DataType == typeof(bool))
                    {
                        root.SetAttribute(Keywords.DEFAULT, (bool)(col.DefaultValue) ? Keywords.TRUE : Keywords.FALSE);
                    }
                    else
                    {
                        if (!col.IsCustomType)
                        { // built in type
                            root.SetAttribute(Keywords.DEFAULT, col.ConvertObjectToXml(col.DefaultValue));
                        }
                        else
                        { // UDT column
                        }
                    }
                }
            }

            if (_schFormat == SchemaFormat.Remoting)
                root.SetAttribute(Keywords.TARGETNAMESPACE, Keywords.MSDNS, col.Namespace);

            else
            {
                if ((col.Namespace != (col.Table.TypeName.IsEmpty ? col.Table.Namespace : col.Table.TypeName.Namespace)) && (col.Namespace.Length != 0))
                {
                    XmlElement schNode = GetSchema(col.Namespace);
                    if (FindTypeNode(schNode, col.EncodedColumnName) == null)
                        schNode.AppendChild(root);
                    root = _dc.CreateElement(Keywords.XSD_PREFIX, refString, Keywords.XSDNS);
                    root.SetAttribute(Keywords.REF, _prefixes[col.Namespace] + ":" + col.EncodedColumnName);
                    if (col.Table.Namespace != _ds.Namespace)
                    {
                        string prefix = (string)_prefixes[col.Namespace];
                        XmlElement tNode = GetSchema(col.Table.Namespace);
                    }
                }
            }


            minOccurs = (col.AllowDBNull) ? 0 : 1;


            // March 2001 change
            if (col.ColumnMapping == MappingType.Attribute && minOccurs != 0)
                root.SetAttribute(Keywords.USE, Keywords.REQUIRED);


            if (col.ColumnMapping == MappingType.Hidden)
            {
                root.SetAttribute(Keywords.USE, Keywords.PROHIBITED);
            }
            else
                if (col.ColumnMapping != MappingType.Attribute && minOccurs != 1)
                root.SetAttribute(Keywords.MINOCCURS, minOccurs.ToString(CultureInfo.InvariantCulture));

            if ((col.ColumnMapping == MappingType.Element) && fWriteOrdinal)
                root.SetAttribute(Keywords.MSD_ORDINAL, Keywords.MSDNS, col.Ordinal.ToString(CultureInfo.InvariantCulture));

            return root;
        }