示例#1
0
        public void WriteXml(XmlWriter writer, XmlWriteMode mode, bool writeHierarchy)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.WriteXml|API> %d#, mode=%d{ds.XmlWriteMode}\n", ObjectID,  (int)mode);
            try{
                if (this.tableName.Length == 0) {
                    throw ExceptionBuilder.CanNotSerializeDataTableWithEmptyName();
                }
                // Generate SchemaTree and write it out
                if (writer != null) {

                    if (mode == XmlWriteMode.DiffGram) { // FIX THIS
                        // Create and save the updates
                        new NewDiffgramGen(this, writeHierarchy).Save(writer, this);
                    }
                    else {
                        // Create and save xml data
                        if (mode == XmlWriteMode.WriteSchema) {
                            DataSet ds = null;
                            string tablenamespace = this.tableNamespace;
                            if (null == this.DataSet) {
                                ds = new DataSet();
                                // if user set values on DataTable, it isn't necessary
                                // to set them on the DataSet because they won't be inherited
                                // but it is simpler to set them in both places

                                // if user did not set values on DataTable, it is required
                                // to set them on the DataSet so the table will inherit
                                // the value already on the Datatable
                                ds.SetLocaleValue(_culture, _cultureUserSet);
                                ds.CaseSensitive = this.CaseSensitive;
                                ds.Namespace = this.Namespace;
                                ds.RemotingFormat = this.RemotingFormat;
                                ds.Tables.Add(this);
                            }

                            if (writer != null) {
                                XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                                xmldataWriter.Save(writer, /*mode == XmlWriteMode.WriteSchema*/true);
                            }
                            if (null != ds) {
                                ds.Tables.Remove(this);
                                this.tableNamespace = tablenamespace;
                            }
                        }
                        else {
                            XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                            xmldataWriter.Save(writer,/*mode == XmlWriteMode.WriteSchema*/ false);
                        }
                    }
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }
        private void GenerateRow(DataRow row)
        {
            DataRowState rowState = row.RowState;

            switch (rowState)
            {
            case DataRowState.Unchanged:
            case DataRowState.Added:
                return;
            }
            if (!this.fBefore)
            {
                this._xmlw.WriteStartElement("diffgr", "before", "urn:schemas-microsoft-com:xml-diffgram-v1");
                this.fBefore = true;
            }
            DataTable table = row.Table;
            int       count = table.Columns.Count;
            string    str3  = table.TableName + row.rowID.ToString(CultureInfo.InvariantCulture);
            string    str   = null;

            if ((rowState == DataRowState.Deleted) && (row.Table.NestedParentRelations.Length != 0))
            {
                DataRow nestedParentRow = row.GetNestedParentRow(DataRowVersion.Original);
                if (nestedParentRow != null)
                {
                    str = nestedParentRow.Table.TableName + nestedParentRow.rowID.ToString(CultureInfo.InvariantCulture);
                }
            }
            string prefix = (table.Namespace.Length != 0) ? table.Prefix : string.Empty;

            if (table.XmlText != null)
            {
                object obj1 = row[table.XmlText, DataRowVersion.Original];
            }
            this._xmlw.WriteStartElement(prefix, row.Table.EncodedTableName, row.Table.Namespace);
            this._xmlw.WriteAttributeString("diffgr", "id", "urn:schemas-microsoft-com:xml-diffgram-v1", str3);
            if ((rowState == DataRowState.Deleted) && XmlDataTreeWriter.RowHasErrors(row))
            {
                this._xmlw.WriteAttributeString("diffgr", "hasErrors", "urn:schemas-microsoft-com:xml-diffgram-v1", "true");
            }
            if (str != null)
            {
                this._xmlw.WriteAttributeString("diffgr", "parentId", "urn:schemas-microsoft-com:xml-diffgram-v1", str);
            }
            this._xmlw.WriteAttributeString("msdata", "rowOrder", "urn:schemas-microsoft-com:xml-msdata", this.rowsOrder[row].ToString());
            for (int i = 0; i < count; i++)
            {
                if ((row.Table.Columns[i].ColumnMapping == MappingType.Attribute) || (row.Table.Columns[i].ColumnMapping == MappingType.Hidden))
                {
                    this.GenerateColumn(row, row.Table.Columns[i], DataRowVersion.Original);
                }
            }
            for (int j = 0; j < count; j++)
            {
                if ((row.Table.Columns[j].ColumnMapping == MappingType.Element) || (row.Table.Columns[j].ColumnMapping == MappingType.SimpleContent))
                {
                    this.GenerateColumn(row, row.Table.Columns[j], DataRowVersion.Original);
                }
            }
            this._xmlw.WriteEndElement();
        }
        private void GenerateColumn(DataRow row, DataColumn col, DataRowVersion version)
        {
            string columnValueAsString = null;

            columnValueAsString = col.GetColumnValueAsString(row, version);
            if (columnValueAsString == null)
            {
                if (col.ColumnMapping == MappingType.SimpleContent)
                {
                    this._xmlw.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
                }
            }
            else
            {
                bool   flag;
                string prefix = (col.Namespace.Length != 0) ? col.Prefix : string.Empty;
                switch (col.ColumnMapping)
                {
                case MappingType.Element:
                {
                    flag = true;
                    object obj2 = row[col, version];
                    if ((!col.IsCustomType || !col.IsValueCustomTypeInstance(obj2)) || typeof(IXmlSerializable).IsAssignableFrom(obj2.GetType()))
                    {
                        this._xmlw.WriteStartElement(prefix, col.EncodedColumnName, col.Namespace);
                        flag = false;
                    }
                    Type type = obj2.GetType();
                    if (col.IsCustomType)
                    {
                        if ((obj2 != DBNull.Value) && (!col.ImplementsINullable || !DataStorage.IsObjectSqlNull(obj2)))
                        {
                            if (col.IsValueCustomTypeInstance(obj2))
                            {
                                if (!flag && (obj2.GetType() != col.DataType))
                                {
                                    this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", DataStorage.GetQualifiedName(type));
                                }
                                if (!flag)
                                {
                                    col.ConvertObjectToXml(obj2, this._xmlw, null);
                                }
                                else
                                {
                                    if (obj2.GetType() != col.DataType)
                                    {
                                        throw ExceptionBuilder.PolymorphismNotSupported(type.AssemblyQualifiedName);
                                    }
                                    XmlRootAttribute xmlAttrib = new XmlRootAttribute(col.EncodedColumnName)
                                    {
                                        Namespace = col.Namespace
                                    };
                                    col.ConvertObjectToXml(obj2, this._xmlw, xmlAttrib);
                                }
                            }
                            else
                            {
                                if (((type == typeof(Type)) || (type == typeof(Guid))) || ((type == typeof(char)) || DataStorage.IsSqlType(type)))
                                {
                                    this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", type.FullName);
                                }
                                else if (obj2 is Type)
                                {
                                    this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", "Type");
                                }
                                else
                                {
                                    string str3 = "xs:" + XmlTreeGen.XmlDataTypeName(type);
                                    this._xmlw.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", str3);
                                    this._xmlw.WriteAttributeString("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
                                }
                                if (!DataStorage.IsSqlType(type))
                                {
                                    this._xmlw.WriteString(col.ConvertObjectToXml(obj2));
                                }
                                else
                                {
                                    col.ConvertObjectToXml(obj2, this._xmlw, null);
                                }
                            }
                        }
                        break;
                    }
                    if (((type == typeof(char)) || (type == typeof(string))) && XmlDataTreeWriter.PreserveSpace(columnValueAsString))
                    {
                        this._xmlw.WriteAttributeString("xml", "space", "http://www.w3.org/XML/1998/namespace", "preserve");
                    }
                    this._xmlw.WriteString(columnValueAsString);
                    break;
                }

                case MappingType.Attribute:
                    this._xmlw.WriteAttributeString(prefix, col.EncodedColumnName, col.Namespace, columnValueAsString);
                    return;

                case MappingType.SimpleContent:
                    this._xmlw.WriteString(columnValueAsString);
                    return;

                case MappingType.Hidden:
                    this._xmlw.WriteAttributeString("msdata", "hidden" + col.EncodedColumnName, "urn:schemas-microsoft-com:xml-msdata", columnValueAsString);
                    return;

                default:
                    return;
                }
                if (!flag)
                {
                    this._xmlw.WriteEndElement();
                }
            }
        }
示例#4
0
        public void WriteXml(XmlWriter writer, XmlWriteMode mode, bool writeHierarchy)
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.WriteXml|API> {0}, mode={1}", ObjectID, mode);
            try
            {
                if (_tableName.Length == 0)
                {
                    throw ExceptionBuilder.CanNotSerializeDataTableWithEmptyName();
                }
                // Generate SchemaTree and write it out
                if (writer != null)
                {
                    if (mode == XmlWriteMode.DiffGram)
                    {
                        // Create and save the updates
                        new NewDiffgramGen(this, writeHierarchy).Save(writer, this);
                    }
                    else
                    {
                        // Create and save xml data
                        if (mode == XmlWriteMode.WriteSchema)
                        {
                            DataSet ds = null;
                            string tablenamespace = _tableNamespace;
                            if (null == DataSet)
                            {
                                ds = new DataSet();

                                // if user set values on DataTable, it isn't necessary
                                // to set them on the DataSet because they won't be inherited
                                // but it is simpler to set them in both places

                                // if user did not set values on DataTable, it is required
                                // to set them on the DataSet so the table will inherit
                                // the value already on the Datatable
                                ds.SetLocaleValue(_culture, _cultureUserSet);
                                ds.CaseSensitive = CaseSensitive;
                                ds.Namespace = Namespace;
                                ds.RemotingFormat = RemotingFormat;
                                ds.Tables.Add(this);
                            }

                            if (writer != null)
                            {
                                XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                                xmldataWriter.Save(writer, /*mode == XmlWriteMode.WriteSchema*/true);
                            }
                            if (null != ds)
                            {
                                ds.Tables.Remove(this);
                                _tableNamespace = tablenamespace;
                            }
                        }
                        else
                        {
                            XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                            xmldataWriter.Save(writer,/*mode == XmlWriteMode.WriteSchema*/ false);
                        }
                    }
                }
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }