/// <summary>
        /// Automatics the detect mapping.
        /// </summary>
        /// <param name="xDoc">The x document.</param>
        /// <returns></returns>
        private XPathMappingList AutoDetectMapping(XDocument xDoc)
        {
            var xPathMappingList = new XPathMappingList();

            if (xDoc == null)
            {
                return(xPathMappingList);
            }

            var nsMgr = this.CreateXmlNamespaceManager(xDoc);

            var xpaths = XPathUtil.GetAllXPaths(xDoc, includeElementPaths: true, includeAttributePaths: true, nsMgr: nsMgr);

            foreach (var xpath in xpaths)
            {
                var xPathMapping = new XPathMapping()
                {
                    Column = this.BuildColumnNameFromXpath(xpath),
                    XPath  = xpath
                };
                xPathMappingList.Add(xPathMapping);
            }

            return(xPathMappingList);
        }
Exemple #2
0
        private XPathMappingList AutoDetectMapping(DataTable table)
        {
            var xPathMappingList = new XPathMappingList();

            foreach (DataColumn column in table.Columns)
            {
                var xPathMapping = new XPathMapping()
                {
                    Column = column.ColumnName,
                    XPath  = this.BuildXpathFromColumnName(column.ColumnName)
                };
                xPathMappingList.Add(xPathMapping);
            }

            return(xPathMappingList);
        }
        private void ExtractXPathMappingFromAttributes <TObj>(XPathMappingList xPathMappings) where TObj : class
        {
            var dataFieldsProps = typeof(TObj).GetProperties()
                                  .Where(p => Attribute.IsDefined(p, typeof(DataFieldAttribute)))
                                  .ToList();

            foreach (var dataFieldProp in dataFieldsProps)
            {
                var attr         = dataFieldProp.GetCustomAttribute <DataFieldAttribute>(false);
                var xPathMapping = new XPathMapping()
                {
                    XPath  = attr.XPath,
                    Column = !string.IsNullOrEmpty(attr.Name)
                                        ? attr.Name
                                        : dataFieldProp.Name
                };

                xPathMappings.Add(xPathMapping);
            }
        }