/// <summary>Helper method to embed delimeted text file into XmlDocument for import into CRM</summary> /// <param name="text"></param> /// <param name="delimeter"></param> /// <returns></returns> private static XmlDocument TextFileToXmlSerialized(string text, char delimeter) { if (string.IsNullOrWhiteSpace(text)) { throw new ArgumentNullException("text", "Data string is empty, cannot compose serialized block"); } if (delimeter == '\0') { var reader = new StringReader(text); if (reader.ReadLine() != null) // Första raden innehöll nåt { var cols = reader.ReadLine(); // Läs andra raden if (cols != null && cols.StartsWith("Entity") && cols.Length > 6) { delimeter = cols[6]; } } } if (delimeter == '\0') { throw new ArgumentNullException("delimeter", "Cannot parse delimeter from data string"); } var xml = new XmlDocument(); XmlNode root = xml.CreateElement("ShuffleData"); xml.AppendChild(root); XML.AppendAttribute(root, "Type", SerializationType.Text.ToString()); XML.AppendAttribute(root, "Delimeter", delimeter.ToString()); XML.AddCDATANode(root, "Text", text); return(xml); }
/// <summary> /// Serialize blocks with entities with given serialization type /// </summary> /// <param name="container"></param> /// <param name="blocks"></param> /// <param name="type"></param> /// <param name="delimeter">Optional, only required for SerializationType: Text</param> /// <returns></returns> public XmlDocument Serialize(IExecutionContainer container, ShuffleBlocks blocks, SerializationType type, char delimeter) { container.StartSection("Serialize"); XmlDocument xml = null; if (blocks.Count > 0) { SendLine(container, "Serializing {0} blocks with type {1}", blocks.Count, type); xml = new XmlDocument(); XmlNode root = xml.CreateElement("ShuffleData"); xml.AppendChild(root); XML.AppendAttribute(root, "Type", type.ToString()); XML.AppendAttribute(root, "ExportTime", DateTime.Now.ToString("s")); switch (type) { case SerializationType.Full: case SerializationType.Simple: case SerializationType.SimpleWithValue: case SerializationType.SimpleNoId: case SerializationType.Explicit: foreach (var block in blocks.Keys) { SendLine(container, $"Serializing {blocks[block].Count()} records in block {block}"); XmlNode xBlock = xml.CreateElement("Block"); root.AppendChild(xBlock); XML.AppendAttribute(xBlock, "Name", block); XML.AppendAttribute(xBlock, "Count", blocks[block].Count().ToString()); var xSerialized = blocks[block].Serialize(container, (SerializationStyle)type); xBlock.AppendChild(xml.ImportNode(xSerialized.ChildNodes[0], true)); } break; case SerializationType.Text: XML.AppendAttribute(root, "Delimeter", delimeter.ToString()); var text = new StringBuilder(); foreach (var block in blocks.Keys) { SendLine(container, $"Serializing {blocks[block].Count()} records in block {block}"); text.AppendLine("<<<" + block + ">>>"); var serializedblock = blocks[block].ToTextFile(container, delimeter); text.Append(serializedblock); } XML.AddCDATANode(root, "Text", text.ToString()); break; } } container.EndSection(); return(xml); }