/// <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); }
public void WriteData(DataTable table, string fileName, string xPath) { var xmlDoc = new XmlDocument(); var namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable); var isNewFile = this.IsNewFile(fileName); if (isNewFile) { // add Declaration XmlNode docNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xmlDoc.AppendChild(docNode); // create base Path XPathUtil.CreateXPath(xmlDoc, xPath); } else { xmlDoc.Load(fileName); } var xmlLines = this.formatter.Format(table, null) as IEnumerable <string>; int writtenRows = 0; int rowIdx = 0; if (xmlLines != null) { foreach (var xmlLine in xmlLines) { var lastNode = xmlDoc.SelectSingleNode(xPath + "[last()]", namespaceMgr); if (lastNode != null) { // Append xml to the last node var xmlDocFragment = xmlDoc.CreateDocumentFragment(); xmlDocFragment.InnerXml = xmlLine; lastNode.AppendChild(xmlDocFragment); writtenRows++; } rowIdx++; } } var settings = new XmlWriterSettings { Indent = true }; using (XmlWriter writer = XmlWriter.Create(fileName, settings)) { xmlDoc.Save(writer); writer.Close(); } }
public override object Format(object data, object existingData = null) { var table = data as DataTable; var headerLine = existingData as string; var xmlLines = new List <string>(); if (table != null) { if (this.XPathMappings == null || this.XPathMappings.Count == 0) { this.AutoDetectSettings(table); } int rowIdx = 0; foreach (DataRow row in table.Rows) { XmlDocument xmlDoc = new XmlDocument(); foreach (var xPathMapping in this.XPathMappings) { var value = row.GetField <string>(xPathMapping.Column, this.DefaultCulture); if (value != null) { var subValues = value.Split(new string[] { "#" }, StringSplitOptions.None); if (subValues.Length > 1) { var index = 1; foreach (var subValue in subValues) { string indexedXpath = xPathMapping.XPath.Replace("/@", "[" + index + "]/@"); XPathUtil.CreateXPath(xmlDoc, indexedXpath, subValue); index++; } } else { XPathUtil.CreateXPath(xmlDoc, xPathMapping.XPath, value); } } else { XPathUtil.CreateXPath(xmlDoc, xPathMapping.XPath, value); } } xmlLines.Add(xmlDoc.OuterXml); rowIdx++; } } return(xmlLines); }
static XmlExtensions() { xPath = new XPathUtil(); }
public override bool WriteData(IEnumerable <DataTable> tables, bool deleteBefore = false) { this.ValidateAndThrow(); var xmlDoc = new XmlDocument(); var namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable); if (!string.IsNullOrEmpty(this.FileName)) { DirectoryUtil.CreateDirectoryIfNotExists(Path.GetDirectoryName(this.FileName)); if (deleteBefore) { FileUtil.DeleteFileIfExists(this.FileName); } if (this.IsNewFile(this.FileName)) { // add Declaration XmlNode docNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xmlDoc.AppendChild(docNode); // create base Path XPathUtil.CreateXPath(xmlDoc, this.XPath); } else { xmlDoc.Load(this.FileName); } } foreach (DataTable table in tables) { var xmlLines = this.WriteFormatter.Format(table, null) as IEnumerable <string>; int writtenRows = 0; int rowIdx = 0; if (xmlLines != null) { foreach (var xmlLine in xmlLines) { var lastNode = xmlDoc.SelectSingleNode(this.XPath + "[last()]", namespaceMgr); if (lastNode != null) { // Append xml to the last node var xmlDocFragment = xmlDoc.CreateDocumentFragment(); xmlDocFragment.InnerXml = xmlLine; lastNode.AppendChild(xmlDocFragment); writtenRows++; } rowIdx++; } } } var settings = new XmlWriterSettings { Indent = true }; XmlWriter writer = null; if (!string.IsNullOrEmpty(this.FileName)) { writer = XmlWriter.Create(this.FileName, settings); } else if (this.DataStream != null) { writer = XmlWriter.Create(this.DataStream, settings); } else { return(false); } xmlDoc.Save(writer); writer.Close(); writer.Dispose(); return(true); }