public XmlToCsvUsingDataSet(string xmlSourceFilePath, bool autoRenameWhenNamingConflict) { XmlDataSet = new DataSet(); try { XmlDataSet.ReadXml(xmlSourceFilePath); foreach (DataTable table in XmlDataSet.Tables) { TableNameCollection.Add(table.TableName); } } catch (DuplicateNameException) { if (autoRenameWhenNamingConflict) { XmlDataSet.ReadXml(xmlSourceFilePath, XmlReadMode.IgnoreSchema); foreach (DataTable table in XmlDataSet.Tables) { TableNameCollection.Add(table.TableName); } RenameDuplicateColumn(); } else { throw; } } }
public XmlToCsvUsingLinq(string xmlSourceFilePath) { _xmlSourceFilePath = @xmlSourceFilePath; var ds = new DataSet("ds"); ds.ReadXmlSchema(@_xmlSourceFilePath); foreach (DataTable table in ds.Tables) { TableNameCollection.Add(table.TableName); } }
/// <summary> /// Check for duplicates names in XML. Rename the table in case a clash with a column name is found. /// </summary> /// <returns>True if a duplicate XML name was found and renames the name clash. Otherwise returns false.</returns> private void RenameDuplicateColumn() { foreach (DataTable table in XmlDataSet.Tables) { bool hasDuplicate = XmlDataSet.Tables[0].Columns.Contains(table.TableName); if (hasDuplicate) { TableNameCollection.Remove(table.TableName); TableNameCollection.Add(table.TableName + "_Renamed"); table.TableName = table.TableName + "_Renamed"; } } }
public XmlToCsvUsingDataSetFromString(XmlDocument xmldoc, string qualifySep, string prefix) { XmlDataSet = new DataSet(); this.prefix(xmldoc.FirstChild, prefix); if (qualifySep != null) { XDocument xdoc = xmldoc.ToXDocument(); this.qualify(xdoc.Elements().First(), qualifySep); xmldoc = xdoc.ToXmlDocument(); } byte[] byteArray = Encoding.ASCII.GetBytes(xmldoc.OuterXml); MemoryStream stream = new MemoryStream(byteArray); try { XmlDataSet.ReadXml(stream); foreach (DataTable table in XmlDataSet.Tables) { TableNameCollection.Add(table.TableName); } } catch (ArgumentException) { stream.Position = 0; XmlDataSet.ReadXml(stream, XmlReadMode.IgnoreSchema); foreach (DataTable table in XmlDataSet.Tables) { TableNameCollection.Add(table.TableName); } RenameDuplicateColumn(); } }