File.readallline在C#中返回特殊字符 [英] File.readallline returning special characters in C#

查看:555
本文介绍了File.readallline在C#中返回特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我正在尝试阅读word文件.docx和文本文件.txt。



为此,我使用此代码:



我尝试过:



Dear All,

I'm trying to read word file ".docx" and text file ".txt".

For this i'm using this code:

What I have tried:

string[] linesFile = File.ReadAllLines(@"" + file, Encoding.UTF8);//word file





输出



Output

"PK\u0003\u0004\u0014\0\u0006\0\b\0\0\0!\0ߤ�lZ\u0001\0\0 \u0005\0\0\u0013\0\b\u0002[Content_Types].xml �\u0004\u0002(�\0\u0002\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0���n�0\u0010E���\u000f��Ub袪*\u0002�>�-R�\a\u0018{\u0002V��Ǽ��\u0013\u0002QU\u0001�"





我只想用任何一个扩展名(。docx/。doc/ etc)从文件中获取文本。



任何人都可以帮助我。





提前致谢



I just want to fetch only text from file with any one of the extension(".docx"/".doc"/etc).

can any one please help me.


Thanks in advance

推荐答案

Word文件不是文本文件。旧Word文件( *。doc )是使用MS复合文件格式的二进制文件。新文件( *。docx )是包含多个文件的压缩档案,其中大多数是XML文件(例如,可能还有二进制图像文件)。



因此,您可以首先解压缩 docx 文件,然后读取存档中包含的每个文件的内容(其中XML文件是可以使用 ReadAllLines <读取的文本文件/ code>)。
Word files are not text files. Old Word files (*.doc) are binary files using the MS compound file format. New files (*.docx) are zipped archives containing multiple files where most are XML files (there may be also binary image files for example).

So you might unzip the docx file first and then read the content of each file contained in the archive (where XML files are text files that can be read with ReadAllLines).


您需要使用 Microsoft.Office.Interop.Word名称空间() [ ^ ]从.doc / docx文件中获取文本。
You need to use the Microsoft.Office.Interop.Word namespace ()[^] to get the text from .doc/docx files.


这篇关于File.readallline在C#中返回特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆