/// <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);
        }
        private DataTable FormatToDataTable(string xmlData, DataTable table = null)
        {
            if (table == null)
            {
                table = new DataTable();
            }

            if (string.IsNullOrEmpty(xmlData))
            {
                return(table);
            }

            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlData);

            if (xmlDoc.DocumentElement != null)
            {
                table.TableName = xmlDoc.DocumentElement.Name;
            }

            var y = 0;

            var rowXPath         = this.FormatterOptions.GetValueOrDefault <string>("RowXPath", "/");
            var removeNamespaces = this.FormatterOptions.GetValueOrDefault <bool>("RemoveNamespaces", false);

            if (removeNamespaces)
            {
                xmlDoc = XmlHelper.RemoveXmlNamespaces(xmlDoc);
            }
            try
            {
                foreach (XmlNode rowNode in xmlDoc.SelectNodes(rowXPath))
                {
                    var xPathMappings = this.XPathMappings;
                    if (xPathMappings == null || xPathMappings.Count == 0)
                    {
                        xPathMappings = this.AutoDetectMapping(rowNode);
                    }

                    var rowValues = this.ReadRowValues(rowNode, xPathMappings);

                    table.AddRow(rowValues, checkForMissingColumns: true);

                    y++;
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("The XPath '{0}' was not found in:{1}{1}{2}", rowXPath, Environment.NewLine, xmlDoc.OuterXml), ex);
            }

            return(table);
        }
예제 #3
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);
        }
예제 #4
0
        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);
            }
        }
        /// <summary>
        /// Reads the row values.
        /// </summary>
        /// <param name="xmlRow">The XML row.</param>
        /// <param name="xPathMappings">The x path mappings.</param>
        /// <returns>
        /// the rowdata as dictionary
        /// </returns>
        private Dictionary <string, object> ReadRowValues(XmlNode xmlRow, XPathMappingList xPathMappings)
        {
            var rowValues = new Dictionary <string, object>();

            var xdoc = new XmlDocument();

            xdoc.LoadXml(xmlRow.OuterXml);
            // get the name of the root node e.g. <book>
            var entityName = xdoc.FirstChild != null
                                    ? xdoc.FirstChild.Name
                                    : "";

            var nsMgr = this.CreateXmlNamespaceManager(xdoc);

            foreach (var xPathMapping in xPathMappings)
            {
                var xPath = xPathMapping.XPath;

                // "/book" -> "/book/title"
                if (!string.IsNullOrEmpty(entityName) && !xPath.StartsWith("/" + entityName))
                {
                    xPath = "/" + entityName + xPath;
                }

                // add all values from the path to the column
                var nodes = xdoc.SelectNodes(xPath, nsMgr);
                if (nodes != null)
                {
                    foreach (XmlNode node in nodes)
                    {
                        rowValues.AddOrAppend(xPathMapping.Column, node != null
                                                                    ? node.GetInnerText("|")
                                                                    : string.Empty, "#");
                    }
                }
            }

            return(rowValues);
        }