System.Data.DataTable.WriteXmlSchema C# (CSharp) Method

WriteXmlSchema() public method

public WriteXmlSchema ( XmlWriter writer, bool writeHierarchy ) : void
writer XmlWriter
writeHierarchy bool
return void
        public void WriteXmlSchema(XmlWriter writer, bool writeHierarchy)
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.WriteXmlSchema|API> {0}", ObjectID);
            try
            {
                if (_tableName.Length == 0)
                {
                    throw ExceptionBuilder.CanNotSerializeDataTableWithEmptyName();
                }

                if (!CheckForClosureOnExpressions(this, writeHierarchy))
                {
                    throw ExceptionBuilder.CanNotSerializeDataTableHierarchy();
                }

                DataSet ds = null;
                string tablenamespace = _tableNamespace;//SQL BU Defect Tracking 286968

                // Generate SchemaTree and write it out
                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)
                {
                    XmlTreeGen treeGen = new XmlTreeGen(SchemaFormat.Public);
                    treeGen.Save(null, this, writer, writeHierarchy);
                }
                if (null != ds)
                {
                    ds.Tables.Remove(this);
                    _tableNamespace = tablenamespace;
                }
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }

Same methods

DataTable::WriteXmlSchema ( Stream stream ) : void
DataTable::WriteXmlSchema ( Stream stream, bool writeHierarchy ) : void
DataTable::WriteXmlSchema ( TextWriter writer ) : void
DataTable::WriteXmlSchema ( TextWriter writer, bool writeHierarchy ) : void
DataTable::WriteXmlSchema ( XmlWriter writer ) : void
DataTable::WriteXmlSchema ( string fileName ) : void
DataTable::WriteXmlSchema ( string fileName, bool writeHierarchy ) : void

Usage Example

Example #1
1
        static void Main(string[] args)
        {
     // Создание Таблицы DataTable с именем "Cars"
            DataTable tablCars = new DataTable("Cars");
            // Создание столбцов
            DataColumn carsId = new DataColumn("Id", typeof(int));
            DataColumn carsName = new DataColumn("Name", typeof(string));
            DataColumn carsCountry = new DataColumn("Country", typeof(string));
            DataColumn carsPrice = new DataColumn("Price", typeof(double));
            tablCars.Columns.AddRange(new DataColumn[] { carsId, carsName, carsCountry, carsPrice });
            // Создание строки с данными 
            DataRow newRow1 = tablCars.NewRow();
            newRow1["Name"] = "BMW"; newRow1["Country"] = "Germany";
            newRow1["Price"] = "50000";
            tablCars.Rows.Add(newRow1);

            DataRow newRow2 = tablCars.NewRow();
            newRow2["Name"] = "Audi"; newRow2["Country"] = "Germany";
            newRow2["Price"] = "37500";
            tablCars.Rows.Add(newRow2);

            // Сохранить ТАБЛИЦЫ  tablCars  в виде XML
            tablCars.WriteXml("Cars.xml");
            tablCars.WriteXmlSchema("CarsSchema.xsd");
         

      // Создание Таблицы DataTable с именем "Drivers"
            DataTable tablDrivers = new DataTable("Drivers");
            // Создание столбцов
            DataColumn drId = new DataColumn("Id", typeof(int));
            DataColumn drName = new DataColumn("Name", typeof(string));
            DataColumn drAge = new DataColumn("Age", typeof(string));
            tablDrivers.Columns.AddRange(new DataColumn[] { drId, drName, drAge });
            // Создание строки с данными 
            DataRow newRow3 = tablDrivers.NewRow();
            newRow3["Name"] = "Ivan"; newRow3["Age"] = "33";
            tablDrivers.Rows.Add(newRow3);

            DataSet dataSet = new DataSet("AutoPark");
            dataSet.Tables.AddRange(new DataTable[] { tablCars, tablDrivers});


            // Сохранить DATASET  в виде XML
            dataSet.WriteXmlSchema("AutoParkSchema.xsd");
            dataSet.WriteXml("AutoPark.xml");
            
            
            // Очистить DataSet.
            dataSet.Clear();

            Console.WriteLine("XML успешно сформированы!");

            Console.ReadKey();

        }
All Usage Examples Of System.Data.DataTable::WriteXmlSchema
DataTable