Fityan Aula Fityan Aula - 27 days ago 11
ASP.NET (C#) Question

Get specific value (string) in some file that we used to ReadAllText

Thank you for visiting!! I am a newbie in c# and sorry if I am asking the silly question but I really need to know.
I have this pretending scenario that I want to get some value in some file for example I write the txt file like this:

<p>Hello Foo! I am Fityan, How have you been?</p>
<img src="~/Files/fityan.jpg"/>
<p>Another tag example<p/>


I try to take the tag on the file, so I try to do this:

string fileContent = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "Files/"+item.Article+".txt");
string imgTag = fileContent.IndexOf("<img").ToString();


As you can see, for the result of the code above I got this text:

<img src="~/Files/fityan.jpg"/>
<p>Another tag example<p/>


But I want to take only this part:

<img src="~/Files/fityan.jpg"/>


How can I do that?
I appreciate for the answer, thanks before :)

Answer

You can find index of "/>" or first occurrence of "<p>" in the same way after "<img " and then use Substring function of string. In the substring function you can pass second parameter as difference between two indexes.