static void Convert(RootCommandOptions options) { foreach (var file in options.Files) { var filePath = file.ToString(); // Returns the original path supplied to FileInfo. // Convert from XML if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) { // XML to MTX if (string.Equals(options.Format, "mtx", StringComparison.OrdinalIgnoreCase) || (options.Format is null && filePath.EndsWith(".mtx.xml", StringComparison.OrdinalIgnoreCase))) { var destinationPath = options.Output is not null ? options.Output.FullName : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension. MtxEncoding encoding; if (options.Fpd is not null) { var fpdFile = new FpdFile(options.Fpd.ToString()); encoding = new CharacterMapMtxEncoding(fpdFile.Characters); } else if (options.Fnt is not null) { var fntFile = new FntFile(options.Fnt.ToString()); encoding = new CharacterMapMtxEncoding(fntFile.Characters); } else { encoding = new Utf16MtxEncoding(); } var serializedSource = File.ReadAllText(filePath); var deserializedSource = Utf8XmlSerializer.Deserialize <MtxSerializable>(serializedSource) ?? throw new NullValueException(); var destination = new MtxFile(deserializedSource, encoding); destination.Save(destinationPath); } // XML to CNVRS-TEXT else if (string.Equals(options.Format, "cnvrs-text", StringComparison.OrdinalIgnoreCase) || (options.Format is null && filePath.EndsWith(".cnvrs-text.xml", StringComparison.OrdinalIgnoreCase))) { var destinationPath = options.Output is not null ? options.Output.FullName : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension. var serializedSource = File.ReadAllText(filePath); var deserializedSource = Utf8XmlSerializer.Deserialize <CnvrsTextSerializable>(serializedSource) ?? throw new NullValueException(); var destination = new CnvrsTextFile(deserializedSource); destination.Save(destinationPath); } }
private static IEnumerable <Product> GetProduct(string productName) { try { var searchUrl = string.Format(SearchUrl, productName); var output = GetOutputFromUrl(searchUrl); var p = Utf8XmlSerializer.Deserialize <Products>(output); return(p); } catch (Exception e) { Log.Error("GetProduct - Error.", e); throw new DataAccessException("GetProduct - Error.", e); } }
public string ToXml() { return(Utf8XmlSerializer.Serialize(this)); }