用innertext只能读出 <b cccccc> </b> stu <b dddddd> </d> dent 你的格式有问题
StreamReader sr = new StreamReader("test.txt"); String str = sr.ReadToEnd(); sr.Close(); int index = 0; string res = ""; while ((index = str.IndexOf("</",index+1)) > 0) { int start = str.IndexOf('>', index + 1); int end = str.IndexOf('<', start + 1); if (start > 0 && end > 0) { res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' '); } } Console.WriteLine(res);
修改一下: StreamReader sr = new StreamReader("test.txt"); String str = sr.ReadToEnd(); sr.Close(); int start = 0; string res = ""; while ((start = str.IndexOf('>', start+1)) > 0) { int end = str.IndexOf('<', start + 1); if (end > 0) { res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' '); }else{ res += str.Substring(start + 1).Trim('\n','\r',' '); break; } start = end + 1; } Console.WriteLine(res);
你这个是XML么,根本不符合定义
上面还是有些问题,再修改了一下: StreamReader sr = new StreamReader("test.txt"); String str = sr.ReadToEnd(); sr.Close(); int index = 0; string res = ""; while ((index = str.IndexOf("</",index)) > 0) { int start = str.IndexOf('>', index + 1); int end = str.IndexOf('<', start + 1); if (start > 0 && end > 0) { res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' '); index = end; } else break; } Console.WriteLine(res);
<b cccccc>
</b>
stu
<b dddddd>
</d>
dent
你的格式有问题
String str = sr.ReadToEnd();
sr.Close();
int index = 0;
string res = "";
while ((index = str.IndexOf("</",index+1)) > 0)
{
int start = str.IndexOf('>', index + 1);
int end = str.IndexOf('<', start + 1);
if (start > 0 && end > 0)
{
res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' ');
}
}
Console.WriteLine(res);
StreamReader sr = new StreamReader("test.txt");
String str = sr.ReadToEnd();
sr.Close();
int start = 0;
string res = "";
while ((start = str.IndexOf('>', start+1)) > 0)
{
int end = str.IndexOf('<', start + 1);
if (end > 0)
{
res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' ');
}else{
res += str.Substring(start + 1).Trim('\n','\r',' ');
break;
}
start = end + 1;
}
Console.WriteLine(res);
StreamReader sr = new StreamReader("test.txt");
String str = sr.ReadToEnd();
sr.Close();
int index = 0;
string res = "";
while ((index = str.IndexOf("</",index)) > 0)
{
int start = str.IndexOf('>', index + 1);
int end = str.IndexOf('<', start + 1);
if (start > 0 && end > 0)
{
res += str.Substring(start + 1, end - start - 1).Trim('\n','\r',' ');
index = end;
} else break;
}
Console.WriteLine(res);