Exemplo n.º 1
0
        public static void WriteQueueToXml(IDataQueue queue, Stream fw, string dbName, string tableName)
        {
            DataTable table = ConnTools.DataTableFromStructure(queue.GetRowFormat);

            table.TableName = tableName;

            List <string> fldnames = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                fldnames.Add(col.ColumnName);
            }

            XmlWriter xw = XmlTextWriter.Create(fw, new XmlWriterSettings {
                Encoding = Encoding.UTF8, CheckCharacters = false
            });

            //XmlTextWriter xw = new XmlTextWriter(fw, Encoding.UTF8);
            //xw.Settings = new XmlWriterSettings { CheckCharacters = false };

            xw.WriteStartDocument();
            xw.WriteStartElement(dbName);
            // mono has bug in writing schema
            if (!Core.IsMono)
            {
                table.WriteXmlSchema(xw);
            }
            List <string> ids = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                ids.Add(XmlTool.NormalizeIdentifier(col.ColumnName));
            }
            try
            {
                while (!queue.IsEof)
                {
                    IBedRecord row = queue.GetRecord();
                    xw.WriteStartElement(tableName);
                    for (int i = 0; i < row.FieldCount; i++)
                    {
                        row.ReadValue(i);
                        var type = row.GetFieldType();
                        if (type == TypeStorage.Null)
                        {
                            continue;
                        }
                        xw.WriteElementString(ids[i], XmlTool.ObjectToString(row.GetValue(i)));
                    }
                    xw.WriteEndElement();
                }
            }
            finally
            {
                queue.CloseReading();
            }
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
        }
Exemplo n.º 2
0
        public static void SaveToXml(ITableStructure table, IEnumerable records, XmlWriter xw)
        {
            List <string> ids = new List <string>();

            foreach (IColumnStructure col in table.Columns)
            {
                ids.Add(XmlTool.NormalizeIdentifier(col.ColumnName));
            }

            foreach (IBedRecord rec in records)
            {
                xw.WriteStartElement("Row");
                for (int i = 0; i < rec.FieldCount; i++)
                {
                    rec.ReadValue(i);
                    string xdata = "", xtype = "";
                    if (!GetValueAsXml(rec, ref xtype, ref xdata))
                    {
                        continue;
                    }
                    xw.WriteStartElement(ids[i]);
                    xw.WriteAttributeString("t", xtype);
                    xw.WriteString(xdata);
                    xw.WriteEndElement();
                }
                xw.WriteEndElement();
            }
        }
Exemplo n.º 3
0
        public static IEnumerable <IBedRecord> LoadFromXml(ITableStructure table, XmlReader reader)
        {
            reader.MoveToContent();
            reader.Read();
            reader.MoveToContent();

            Dictionary <string, int> colPos = new Dictionary <string, int>();
            int index = 0;

            foreach (var col in table.Columns)
            {
                colPos[XmlTool.NormalizeIdentifier(col.ColumnName)] = index;
                index++;
            }

            while (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.LocalName != "Row")
                {
                    throw new XmlFormatError(String.Format("DAE-00216 Bad xml, expected tag {0}, found {1}", "Row", reader.LocalName));
                }
                reader.Read();
                reader.MoveToContent();
                object[] values = new object[table.Columns.Count];
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = DBNull.Value;
                }
                while (reader.NodeType == XmlNodeType.Element)
                {
                    string colname = reader.LocalName;
                    int    pos     = colPos[colname];

                    string xtype = reader.GetAttribute("t");
                    reader.Read();
                    string xdata = "";
                    if (reader.NodeType == XmlNodeType.Text)
                    {
                        xdata = reader.Value;
                        reader.Read();
                    }

                    values[pos] = GetValueFromXml(xtype, xdata);

                    if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == colname)
                    { // skip end of element
                        reader.Read();
                    }
                }
                yield return(new ArrayDataRecord(table, values));

                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    reader.Read();
                }
            }
        }
Exemplo n.º 4
0
        public override void FillTable(ITableStructure table, IDataQueue queue, TableCopyOptions opts)
        {
            var          name  = new NameWithSchema(table.FullName.Schema, table.FullName.Name, false);
            var          entry = m_zip.PutNextEntry(XmlTool.NormalizeIdentifier(name.ToString()) + ".drs");
            BedDataStats stats = new BedDataStats();

            BedTool.WriteQueueToStream(queue, m_zip, stats);
            ProgressInfo.LogMessage("s_export", LogLevel.Info, Texts.Get("s_exported$table$rows$bytes", "rows", stats.Rows, "bytes", stats.Bytes, "table", table.FullName));
            //ZipEntry entry = new ZipEntry(XmlTool.NormalizeIdentifier(table.FullName.ToString()) + ".xml");
            //m_zip.PutNextEntry(entry);
            //XmlTool.WriteQueueToXml(queue, m_zip, "DataSet", "DataRow");
        }
Exemplo n.º 5
0
 private void DoReadTable(NameWithSchema table, IDataQueue queue)
 {
     try
     {
         string   fnbase = XmlTool.NormalizeIdentifier(table.ToString());
         ZipEntry xmlEntry;
         try { xmlEntry = m_zip[fnbase + ".xml"]; }
         catch { xmlEntry = null; }
         ZipEntry drsEntry;
         try { drsEntry = m_zip[fnbase + ".drs"]; }
         catch { drsEntry = null; }
         if (drsEntry == null && xmlEntry == null)
         {
             var dbs = GetStructure();
             if (dbs.Tables.GetIndex(table) < 0)
             {
                 throw new InternalError("DAE-00019 Table not found in data archive:" + table.ToString());
             }
             // table is empty, only has no drs nor xml file
             queue.PutEof();
             return;
         }
         using (Stream fr = (drsEntry ?? xmlEntry).OpenReader())
         {
             if (drsEntry != null)
             {
                 BedTool.LoadQueue(fr, queue);
             }
             else if (xmlEntry != null)
             {
                 using (XmlReader reader = XmlReader.Create(fr, new XmlReaderSettings {
                     CheckCharacters = false
                 }))
                 {
                     XmlDataTool.ReadXmlToQueue(reader, queue, "DataRow");
                 }
             }
         }
     }
     finally
     {
         queue.CloseWriting();
     }
 }
Exemplo n.º 6
0
        public void WriteStart(StreamWriter fw, ITableStructure table, ref object manager)
        {
            Manager       mgr = new Manager();
            XmlTextWriter xw  = new XmlTextWriter(fw);

            mgr.xw        = xw;
            mgr.formatter = new BedValueFormatter(FormatSettings);
            xw.WriteStartDocument();
            xw.WriteStartElement(m_rootElementName);
            foreach (IColumnStructure col in table.Columns)
            {
                mgr.cols.Add(col);
            }
            foreach (IColumnStructure col in mgr.cols)
            {
                mgr.ids.Add(XmlTool.NormalizeIdentifier(col.ColumnName));
            }
            manager = mgr;
        }
Exemplo n.º 7
0
        public static DataTable ReadXmlToQueue(XmlReader reader, IDataQueue queue, string tableTagName)
        {
            try
            {
                reader.MoveToContent();
                reader.Read();
                reader.MoveToContent();

                DataTable structTable = null;
                if (reader.LocalName != tableTagName)
                {
                    structTable = new DataTable();
                    structTable.ReadXmlSchema(reader);
                }

                Dictionary <string, int> colPos = new Dictionary <string, int>();
                ITableStructure          rowFormat;
                if (structTable != null)
                {
                    foreach (DataColumn col in structTable.Columns)
                    {
                        colPos[XmlTool.NormalizeIdentifier(col.ColumnName)] = col.Ordinal;
                    }
                    rowFormat = structTable.Columns.GetTableStructure(null);
                }
                else
                {
                    rowFormat = queue.PutRowFormat;
                    int index = 0;
                    foreach (var col in rowFormat.Columns)
                    {
                        colPos[XmlTool.NormalizeIdentifier(col.ColumnName)] = index;
                        index++;
                    }
                }
                while (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName != tableTagName)
                    {
                        throw new XmlFormatError(String.Format("DAE-00301 Bad xml, expected tag {0}, found {1}", tableTagName, reader.LocalName));
                    }
                    reader.Read();
                    reader.MoveToContent();
                    object[] values = new object[rowFormat.Columns.Count];
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = DBNull.Value;
                    }
                    while (reader.NodeType == XmlNodeType.Element)
                    {
                        string colname = reader.LocalName;
                        int    pos     = colPos[colname];
                        bool   wasdata = false;

                        Type dataType = structTable != null ? structTable.Columns[pos].DataType : rowFormat.Columns[pos].DataType.DotNetType;
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            string data = reader.Value;
                            values[pos] = XmlTool.StringToObject_DataXml(dataType, data);
                            reader.Read();
                            wasdata = true;
                        }
                        if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == colname)
                        { // skip end of element
                            reader.Read();
                        }
                        else
                        {
                            // error, do not throw, it is error if .NET parser
                        }
                        //if (reader.NodeType != XmlNodeType.EndElement) throw new XmlFormatError("Bad XML, expected end of tag");
                        //if (reader.LocalName != colname) throw new XmlFormatError(String.Format("Bad xml, expected tag {0}, found {1}", colname, reader.LocalName));
                        //reader.Read();

                        if (!wasdata)
                        {
                            values[pos] = XmlTool.StringToObject_DataXml(dataType, "");
                        }
                    }
                    queue.PutRecord(new ArrayDataRecord(rowFormat, values));
                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                }
                return(structTable);
            }
            finally
            {
                queue.PutEof();
            }
        }
Exemplo n.º 8
0
        protected override void DoRead(IDataQueue queue)
        {
            try
            {
                TableStructure s = CreateStructure();

                Dictionary <string, int> colPos = new Dictionary <string, int>();
                for (int i = 0; i < m_columnNames.Count; i++)
                {
                    colPos[m_columnNames[i]] = i;
                    colPos[XmlTool.NormalizeIdentifier(m_columnNames[i])] = i;
                }

                using (XmlReader xr = new XmlTextReader(GetWorkingFileName()))
                {
                    xr.MoveToContent();
                    if (xr.Name != m_rootElementName)
                    {
                        Logging.Warning("Root element has different name");
                    }
                    xr.Read();
                    while (xr.NodeType == XmlNodeType.Element)
                    {
                        // process one row

                        object[] values = new object[m_columnNames.Count];
                        for (int i = 0; i < values.Length; i++)
                        {
                            values[i] = DBNull.Value;
                        }


                        if (xr.Name == m_rowElementName)
                        {
                            switch (m_storageType)
                            {
                            case XmlDataStorageType.Attribute:
                                for (int i = 0; i < xr.AttributeCount; i++)
                                {
                                    xr.MoveToAttribute(i);
                                    string name = xr.Name;
                                    if (colPos.ContainsKey(name))
                                    {
                                        values[colPos[name]] = xr.Value;
                                    }
                                }
                                xr.MoveToElement();
                                xr.Read();
                                break;

                            case XmlDataStorageType.ColumnNamedElement:
                                xr.Read();
                                xr.MoveToContent();
                                while (xr.NodeType == XmlNodeType.Element)
                                {
                                    string name  = xr.Name;
                                    string value = xr.ReadElementContentAs(typeof(string), null).ToString();
                                    if (colPos.ContainsKey(name))
                                    {
                                        values[colPos[name]] = value;
                                    }
                                }
                                xr.MoveToContent();
                                if (xr.NodeType == XmlNodeType.EndElement)
                                {
                                    xr.Read();
                                }
                                break;

                            case XmlDataStorageType.InvariantNamedElement:
                                xr.Read();
                                xr.MoveToContent();
                                while (xr.NodeType == XmlNodeType.Element)
                                {
                                    string name  = xr.GetAttribute(m_columnAttributeName);
                                    string value = xr.ReadElementContentAs(typeof(string), null).ToString();
                                    if (colPos.ContainsKey(name))
                                    {
                                        values[colPos[name]] = value;
                                    }
                                }
                                xr.MoveToContent();
                                if (xr.NodeType == XmlNodeType.EndElement)
                                {
                                    xr.Read();
                                }
                                break;
                            }
                        }
                        else
                        {
                            xr.Skip();
                        }

                        queue.PutRecord(new ArrayDataRecord(s, values));
                    }
                }
            }
            finally
            {
                queue.PutEof();
            }
            FinalizeBulkCopy();
        }