sudheer kumar

sudheer kumar

  • 2k
  • 43
  • 1.1k

Creating .dat file using binary writer

Aug 6 2019 4:05 AM
I want to create a file in .dat format using binary writer.
Currently we are creating in .xml format same way now we want to give user option of creating.xml or .dat based on user selection we have create the file. ExportCommand wil create the filw afetr that when user click on open/save file will be downloaded in user system.
 
My curent code for xml file creation  is
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExportCommand(MyMedical.SpeechCommand command, string fileName)
{
using (var activity = new LogActivityScope(1, LogActivyScopeOptions.Boundries, "ExportCommand [{0}]", sessionname))
{
try
{
XmlDocument doc = new XmlDocument();
using (MemoryStream inputStream = new MemoryStream(MyFilestore.Instance.ReadFile(fileName)))
{
doc.XmlResolver = null;
doc.Load(inputStream);
XElement cmd = new XElement("SpeechCommand",
new XElement("Type", "RecordedMacro"),
new XElement("Name", command.Name),
new XElement("Description", !string.IsNullOrWhiteSpace(command.Description) ? command.Description : string.Empty),
new XElement("OwningObjectType", command.OwnerType),
new XElement("OwningObjectName", command.OwnerName),
new XElement("ProductGuid", My.Common.ProductGuids.StreamingSpeechSDK.ToString()),
new XElement("Locale", command.Locale));
//MetaDataDictionary
if (command.MetaDataDictionary != null && command.MetaDataDictionary.Count() != 0)
{
XElement metaDatasElement = new XElement("MetaDataDictionary");
foreach (MyDragonMedical.SpeechMetaData metaData in command.MetaDataDictionary)
{
XElement metadataElement = new XElement("MetaData");
metadataElement.Add(new XElement("Name", metaData.Name));
metadataElement.Add(new XElement("Value", metaData.Value));
metaDatasElement.Add(metadataElement);
}
cmd.Add(metaDatasElement);
}
if (command.AlternateNames != null && command.AlternateNames.Count() != 0)
{
XElement spokenPhrasesElement = new XElement("SpokenPhrases");
for (int i = 0; i < command.AlternateNames.Count(); i++)
{
XElement spokenPhraseElement = new XElement("SpokenPhrase");
spokenPhraseElement.Add(new XElement("Phrase", command.AlternateNames.ElementAt(i).SpokenForm));
spokenPhraseElement.Add(new XElement("DisplayString", command.AlternateNames.ElementAt(i).DisplayString));
spokenPhrasesElement.Add(spokenPhraseElement);
}
cmd.Add(spokenPhrasesElement);
}
MyMedical.SCBodyResult commandBody = Gettingocmmandbody from service;
cmd.Add(new XElement("Body", commandBody.Body));
doc.DocumentElement.CreateNavigator().AppendChild(cmd.ToString());
using (MemoryStream outputStream = new MemoryStream())
{
doc.Save(outputStream);
MyFilestore.Instance.UpdateFile(outputStream, fileName);
}
}
return new EmptyResult();
}
catch (Exception ex)
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return new WrappedJsonResult { Data = _dragonMedicalExceptionHandler.HandleException(ex) };
}
}
}
[HttpGet]
public FileResult DownloadCommandExportFile(string fileName)
{
FileContentResult result = null;
try
{
string timeStamp = String.Format("{0:yyyyMMddHHmmss}", DateTime.Now.ToLocalTime());
string saveFileName = string.Format("exportedCommands_{0}.xml", timeStamp);
result = File(MyFilestore.Instance.ReadFile(fileName), "application/xml", saveFileName);
}
catch (Exception ex)
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
return result;
}
 

Answers (1)