简体   繁体   English

将文件转换保存为变量而不是物理位置

[英]Saving File conversion to variable instead of physical location

I'm trying out to use the XML - Mind converter https://www.xmlmind.com/foconverter/ to convert some xsl-fo to an rtf and this works well.我正在尝试使用 XML - Mind converter https://www.xmlmind.com/foconverter/将一些 xsl-fo 转换为 rtf,这很好用。 Just to be clear this is nothing specific to the conveter or its functionality but just a clarification I would like to get which is why I am asking this on stack overflow.为了清楚起见,这不是特定于转换器或其功能的,而只是我想得到的澄清,这就是为什么我在堆栈溢出时问这个问题。

So I have the following code (that was obtained from some documentation)所以我有以下代码(从一些文档中获得)

string foFilePath = @"D:\Temp\test.fo";
string ourPutFilePath = @"D:\Temp\test.rtf";


                Converter converter = new Converter();
                converter.OutputFormat = OutputFormat.Rtf;
                converter.OutputEncoding = "windows-1252";
                converter.ImageResolution = 120;


                converter.SetInput(foFilePath);
                converter.SetOutput(ourPutFilePath);
                
                converter.Convert();

What happens here is quite simple Reads a file from the input path and stores the converted file in the specified output.这里发生的事情非常简单,从输入路径读取一个文件,并将转换后的文件存储在指定的 output 中。 The question I would like to clarify here is, wheather it would be possible to store this content that is being saved in the file out put path within a variable as well to may be do some processing during the application runtime?我想在这里澄清的问题是,是否可以将保存在文件输出路径中的内容存储在变量中,以便在应用程序运行时进行一些处理?

Maybe I can use the MemoryStream for it?也许我可以使用MemoryStream呢? I'm just not sure how to do it and would really appreciate some help here.我只是不确定该怎么做,非常感谢这里的一些帮助。

I understand that I can always read it back from the file output path but I am looking for something better than that as saving the file to a certain location may not always be possible in my case我知道我总是可以从文件 output 路径中读回它,但我正在寻找比这更好的东西,因为在我的情况下可能并不总是可以将文件保存到某个位置

EDIT:- The converter.SetOutput() method allows 3 overloads in the parameters编辑:- converter.SetOutput() 方法允许参数中的 3 个重载

  1. String fileName字符串文件名
  2. Stream stream Stream stream
  3. TextWriter writer文本作家作家

Sine you need the output as a string you could try doing something like this因为你需要 output 作为字符串,你可以尝试做这样的事情

      string content;
      using (var stream = new MemoryStream())
      {
        using (var writer = new StreamWriter(stream))
        {
          Converter converter = new Converter();
          converter.OutputFormat = OutputFormat.Rtf;
          converter.OutputEncoding = "windows-1252";
          converter.ImageResolution = 120;
          converter.SetInput(foFilePath);
          converter.SetOutput(writer);
          converter.Convert();
          stream.Position = 0;
          content = Encoding.UTF8.GetString(stream.ToArray());
        }
      } 

I'm not sure about the Encoding though, and if the Convert() uses a different encoding this might not work不过我不确定编码,如果Convert()使用不同的编码,这可能不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM