-2

I am using stream reader for reading a text file.

This is the contents of the .txt file:

</a> Schools's are a suitable public </a>

When I read that text I got:

<a>Schoolss are a suitable public<a>

As you can see I did't receive the quotation. How can I receive the special character in a stream reader?

I used following code:

using (StreamReader reader = new StreamReader(CommonGetSet.FileName, System.Text.Encoding.ASCII))
{
    string text = reader.ReadToEnd();
    docKeyword = XDocument.Parse(text);
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Malathi
  • 43
  • 7
  • 2
    Why are you loading it into an xml format if it is text format? – TheLethalCoder May 04 '17 at 08:47
  • Possible duplicate of [How to read an entire file to a string using C#?](http://stackoverflow.com/questions/7387085/how-to-read-an-entire-file-to-a-string-using-c) – TheLethalCoder May 04 '17 at 08:48
  • What do you mean "special character"? What is the error Why are you using `XDocument.Load` if the file doesn't contain XML? It's impossible to help without information – Panagiotis Kanavos May 04 '17 at 08:48
  • If you want to read a text file, use `File.ReadAllText`. This method can detect the encoding by checking the BOM character that precedes most Unicode files – Panagiotis Kanavos May 04 '17 at 08:49
  • @PanagiotisKanavos I believe all the information is in the question itself, they are reading a text file into xml format. Because xml doesn't want to contain the `'` it gets stripped. The question is essentially asking how to read a file in, something which the duplicate shows. – TheLethalCoder May 04 '17 at 08:50
  • @TheLethalCoder then that's more suitable for a tutorial on file IO – Panagiotis Kanavos May 04 '17 at 08:53
  • @PanagiotisKanavos I agree, however, the dupe target essentially shows the user how to read in a text file. – TheLethalCoder May 04 '17 at 08:54
  • 1
    `'` is a special character in XML. See http://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents for why you are having problems parsing this content as XML. – Chris May 04 '17 at 09:04

2 Answers2

2

The problem you are having is that you are trying to load a text file with an xml reader, i.e. this part:

XDocument.Load(reader);

If you look at this question: What characters do I need to escape in XML documents?, you will see other characters that will be stripped/need escaping too.

If you inspect the StreamReader in the debugger you will see it shows the correct text, something that the answer by @JinsPeter shows. So you need to read in a text file, the easiest way is to use either File.ReadAllText or File.ReadAllLines depending on whether you want the result as a string or string[] respectively:

string contents = File.ReadAllText(path);
string[] lines = File.ReadAllLines(path);

However, if for some reason you really want to use a StreamReader you can read directly from the stream using ReadToEnd, ReadLine or any other appropriate read method:

using (StreamReader reader = new StreamReader(path))
{
    string contents = reader.ReadToEnd();
}

However, note that the StreamReader methods will read from the current position in the stream so you may need to set the position yourself.

For a list of other ways to read in a file in C# see this question: How to read an entire file to a string using C#?.

Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • Good answer though I note that while it deals with the question of why this isn't working it may be worth mentioning that the solution might be to fix the file that is being read rather than changing the code. The edit suggests that it may be XML (although if that is meant to be XML and is what the OP has there are many other issues). Of course I have mentioned it here in comments now so you don't necessarily need to add it to the answer any more. :) – Chris May 04 '17 at 09:15
  • 1
    @Chris I agree however, as the OP has changed the file contents once, I believe we probably still don't have the full file. In that case there's no point in guessing and I'll just answer the asked question of, why isn't the way I'm reading in a .txt file working? – TheLethalCoder May 04 '17 at 09:18
  • Yup. Wasn't trying to say your answer was wrong, just that there are two possibilities of what is wrong, one being that they are reading text files wrong and one that they have an invalid xml file. The fact that they talk about it being a text file does suggest you are right to focus on that though. – Chris May 04 '17 at 09:22
1

When I printed the same text inside the StreamReader I got the ' . So the issue is with writing it to XML or HTML. Try to fix that rather than finding issue in StreamReader.

using (StreamReader inputStream = new StreamReader(filepath, System.Text.Encoding.UTF8))
{
    string line = inputStream.ReadToEnd();
    Console.WriteLine(line);
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Jins Peter
  • 2,368
  • 1
  • 18
  • 37
  • Because it is the `XDocument` i.e. xml that is stripping the `'` not the `StreamReader`. – TheLethalCoder May 04 '17 at 08:51
  • The OP is trying to read text using an XML reader. *This* answer is reading it as text, which is why it works. The OP should just *not* use `XDocument` – Panagiotis Kanavos May 04 '17 at 08:52
  • No She has read the file correctly. And the string she is reading to will have the full content including the '. So the issue is about writing it to XML or HMTL. whatever it be. – Jins Peter May 04 '17 at 09:05