コード例 #1
0
ファイル: XmlDataTool.cs プロジェクト: janproch/datadmin
        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();
            }
        }