Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
i have one notepad file like this

XML
Some data..
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>
<div class="info">
                                    208,Kunj Enclave, Opp Krishna Bunglows,Near Ambe School,Beside Vadsar Bridge
                                 </div>


Some data..


now i want some data in string[] format in between
HTML
<div class="info">
and
HTML
</div>
tag so what can i do for it ?
Posted
Updated 12-Jun-13 22:51pm
v2
Comments
CHill60 13-Jun-13 5:18am    
I don't quite understand your problem. You have data between the div tags, it's comma-separated so easy enough to read into a string array. If you post the code you're using to read it in and explain either the error or how the results don't match what you expect
Shine Ashraf 13-Jun-13 6:14am    
Read the data as a XML string and fetch the inner xml of each <div> node.

Try this:
C#
FileStream fs = new FileStream(@"C:\Users\amit\Desktop\Test.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd();
string [] separator = {"<div class=\"info\">", "</div>"};//Store the separator in string array
string[] newStrings = str.Split(separator, StringSplitOptions.None);//Split the string using that seperator. You can again split these values using comma(,) and get the strings.


Hope it helps!
--Amit
 
Share this answer
 
v4
If you're using ASP.NET, and the HTML file is relative to your website path:
C#
string html = File.ReadAllText(Server.MapPath("~/virtualPathTo/file.html"));
int start_index = html.IndexOf("<div class="info">") + 18;
int end_index = html.IndexOf("</div>") + 6;
string[] return1 = html.Substring(start_index, end_index - start_index).Split(,);

html = html.Substring(end_index);        //suppress previous div from string

... (do the same for getting the others div informations)
 
Share this answer
 
By repeated use of System.String.IndexOf()[^] or a one-time System.String.IndexOfAny()[^] you can get all the
XML
<div class="info"></div>
tags. From each one you can then search for the closing
XML

and store the in-between stuff in your string array.

Or, since this seems to be XML, use aSystem.Xml.XmlDocument[^], feed it with one of its Load methods (Load(TextReader)[^], Load(Stream)[^], LoadXml(String)[^], ...) and check its ChildNodes[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900