/// <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') { StringReader reader = new StringReader(text); if (reader.ReadLine() != null) // Första raden innehöll nåt { string 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"); } XmlDocument xml = new XmlDocument(); XmlNode root = xml.CreateElement("ShuffleData"); xml.AppendChild(root); CintXML.AppendAttribute(root, "Type", SerializationType.Text.ToString()); CintXML.AppendAttribute(root, "Delimeter", delimeter.ToString()); CintXML.AddCDATANode(root, "Text", text); return(xml); }
/// <summary> /// Serialize blocks with entities with given serialization type /// </summary> /// <param name="blocks"></param> /// <param name="type"></param> /// <param name="delimeter">Optional, only required for SerializationType: Text</param> /// <returns></returns> public XmlDocument Serialize(ShuffleBlocks blocks, SerializationType type, char delimeter) { log.StartSection("Serialize"); XmlDocument xml = null; if (blocks.Count > 0) { SendLine("Serializing {0} blocks with type {1}", blocks.Count, type); xml = new XmlDocument(); XmlNode root = xml.CreateElement("ShuffleData"); xml.AppendChild(root); CintXML.AppendAttribute(root, "Type", type.ToString()); CintXML.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 (string block in blocks.Keys) { SendLine("Serializing {0} records in block {1}", blocks[block].Count, block); XmlNode xBlock = xml.CreateElement("Block"); root.AppendChild(xBlock); CintXML.AppendAttribute(xBlock, "Name", block); CintXML.AppendAttribute(xBlock, "Count", blocks[block].Count.ToString()); XmlDocument xSerialized = blocks[block].Serialize((SerializationStyle)type); xBlock.AppendChild(xml.ImportNode(xSerialized.ChildNodes[0], true)); } break; case SerializationType.Text: CintXML.AppendAttribute(root, "Delimeter", delimeter.ToString()); StringBuilder text = new StringBuilder(); foreach (string block in blocks.Keys) { SendLine("Serializing {0} records in block {1}", blocks[block].Count, block); text.AppendLine("<<<" + block + ">>>"); string serializedblock = blocks[block].ToTextFile(delimeter); text.Append(serializedblock); } CintXML.AddCDATANode(root, "Text", text.ToString()); break; } } log.EndSection(); return(xml); }