Exemple #1
0
        public static string ToText <TRec>(TRec record, ChoXmlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string xpath = null)
            where TRec : class
        {
            XmlRootAttribute root  = ChoType.GetCustomAttribute <XmlRootAttribute>(typeof(TRec), false);
            string           xPath = null;

            if (record != null && !record.GetType().IsDynamicType())
            {
                xPath = root != null ? root.ElementName : typeof(TRec).Name;
            }
            return(ToTextAll(ChoEnumerable.AsEnumerable <TRec>(record), configuration, traceSwitch, xPath));
        }
Exemple #2
0
        public Type GetSourceTypeFromConvertersIfAny()
        {
            Type srcType = null;

            if (PropConverters != null)
            {
                foreach (var c in PropConverters.Where(c1 => c1 != null))
                {
                    var attr = ChoType.GetCustomAttribute <ChoSourceTypeAttribute>(c.GetType(), true);
                    if (attr != null)
                    {
                        srcType = attr.Type;
                        if (srcType != null)
                        {
                            return(srcType);
                        }
                    }
                }
            }

            if (Converters != null)
            {
                foreach (var c in Converters.Where(c1 => c1 != null))
                {
                    var attr = c.GetType().GetCustomAttribute(typeof(ChoSourceTypeAttribute)) as ChoSourceTypeAttribute;
                    if (attr != null)
                    {
                        srcType = attr.Type;
                        if (srcType != null)
                        {
                            return(srcType);
                        }
                    }
                }
            }

            return(null);
        }
Exemple #3
0
        public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoXmlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string xpath = null)
            where TRec : class
        {
            if (records == null)
            {
                return(null);
            }

            if (typeof(DataTable).IsAssignableFrom(typeof(TRec)))
            {
                StringBuilder xml = new StringBuilder();

                foreach (var dt in records.Take(1))
                {
                    using (var w = new ChoXmlWriter(xml, configuration))
                        w.Write(dt);
                }

                return(xml.ToString());
            }
            else if (typeof(IDataReader).IsAssignableFrom(typeof(TRec)))
            {
                StringBuilder xml = new StringBuilder();

                foreach (var dt in records.Take(1))
                {
                    using (var w = new ChoXmlWriter(xml, configuration))
                        w.Write(dt);
                }

                return(xml.ToString());
            }

            var pe = new ChoPeekEnumerator <TRec>(records, (Func <TRec, bool?>)null);

            if (configuration == null)
            {
                configuration = new ChoXmlRecordConfiguration();
            }

            configuration.IgnoreRootName = false;

            TRec record = pe.Peek;

            if (record != null)
            {
                if (configuration.NodeName.IsNullOrWhiteSpace())
                {
                    ChoDynamicObject rec1 = record as ChoDynamicObject;
                    if (rec1 != null)
                    {
                        configuration.NodeName = rec1.DynamicObjectName;
                        if (configuration.RootName.IsNullOrWhiteSpace())
                        {
                            configuration.RootName = configuration.NodeName.ToPlural();
                        }
                    }
                    else
                    {
                        XmlRootAttribute root     = ChoType.GetCustomAttribute <XmlRootAttribute>(record.GetType(), false);
                        string           nodeName = "XElement";
                        if (root != null && !root.ElementName.IsNullOrWhiteSpace())
                        {
                            nodeName = root.ElementName.Trim();
                        }
                        else
                        {
                            nodeName = record.GetType().Name;
                        }

                        if (configuration.RootName.IsNullOrWhiteSpace())
                        {
                            configuration.RootName = nodeName.ToPlural();
                        }
                        configuration.NodeName = nodeName;
                    }
                }
            }
            else
            {
                if (configuration.RootName.IsNullOrWhiteSpace())
                {
                    configuration.RootName = "Root";
                }
            }

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoXmlWriter <TRec>(writer, configuration)
                        {
                            TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch
                        })
                        {
                            //parser.Configuration.XPath = xpath;

                            parser.Write(pe.ToEnumerable());

                            parser.Close();

                            writer.Flush();
                            stream.Position = 0;

                            return(reader.ReadToEnd());
                        }
        }
Exemple #4
0
        public static string ToText <TRec>(TRec record, ChoXmlRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string xpath = null)
            where TRec : class
        {
            if (record is DataTable)
            {
                StringBuilder xml = new StringBuilder();
                using (var w = new ChoXmlWriter(xml, configuration))
                    w.Write(record as DataTable);
                return(xml.ToString());
            }
            else if (record is IDataReader)
            {
                StringBuilder xml = new StringBuilder();
                using (var w = new ChoXmlWriter(xml, configuration))
                    w.Write(record as IDataReader);
                return(xml.ToString());
            }

            if (configuration == null)
            {
                configuration = new ChoXmlRecordConfiguration(typeof(TRec));
                configuration.IgnoreRootName = true;
                configuration.RootName       = null;
            }

            configuration.IgnoreNodeName = true;

            if (record != null)
            {
                if (configuration.NodeName.IsNullOrWhiteSpace())
                {
                    ChoDynamicObject rec1 = record as ChoDynamicObject;
                    if (rec1 != null)
                    {
                        if (rec1.DynamicObjectName != ChoDynamicObject.DefaultName)
                        {
                            configuration.NodeName = rec1.DynamicObjectName;
                        }
                        else
                        {
                            //configuration.IgnoreNodeName = true;
                            //configuration.NodeName = null;
                        }
                    }
                    else
                    {
                        XmlRootAttribute root     = ChoType.GetCustomAttribute <XmlRootAttribute>(record.GetType(), false);
                        string           nodeName = "XElement";
                        if (root != null && !root.ElementName.IsNullOrWhiteSpace())
                        {
                            nodeName = root.ElementName.Trim();
                        }
                        else
                        {
                            nodeName = record.GetType().Name;
                        }

                        configuration.NodeName = nodeName;
                    }
                }
            }

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoXmlWriter <TRec>(writer, configuration)
                        {
                            TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch
                        })
                        {
                            //parser.Configuration.XPath = xpath;

                            if (record != null)
                            {
                                parser.Write(ChoEnumerable.AsEnumerable <TRec>(record));
                            }

                            parser.Close();

                            writer.Flush();
                            stream.Position = 0;

                            return(reader.ReadToEnd());
                        }
        }