Hi All,
I want to read text from .txt file in between 2 specific fields like
Account_NUMBER>1298406
I want to get '1298406' as. The file which I am using contains multiple records with different account numbers.
Can some one please help me?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Muhammad MasoodPosted Jun 13, 2012, 1:08 AM
I have Done it by below code
string orderNo = "";") == -1)") + 15); ")) + "'" + "\r\n";
int count = 0;
while (true)
{
if (txtFromFiles.IndexOf("
{
break;
}
else
{
txtFromFiles = txtFromFiles.Substring(txtFromFiles.IndexOf("
orderNo += ",'" + txtFromFiles.Substring(0, txtFromFiles.IndexOf("
}
count++;
It gives me all orders in a sequence the exists in the file.
Again thanx alot brothers.
Jignesh TrivediPosted Jun 12, 2012, 8:49 AM
Are you text file contain XML?
if yes please refer
http://www.csharp-examples.net/xml-nodes-by-name/
you can also get it from Regex
string regExp = @"<(.*)Account_NUMBER>(.*)(.*)Account_NUMBER>";//@"(?m)^(\)(.*)$";
string text = File.ReadAllText(@"E:\test11.txt");
string result = Regex.Match(text, regExp).Groups[2].Value;
hope this will help you.Posted Jun 12, 2012, 2:02 AM
Read the xml content by using XMLDocument and read the values Account_number tag values.
XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); // suppose your file content
XmlNodeList xnList = xml.SelectNodes("/Customers/Customer");
foreach (XmlNode xn in xnList)
{
string accnumber = xn["Account_NUMBER"].InnerText;
Console.WriteLine("account number: {0}", accnumber);
}
http://support.microsoft.com/kb/307548
You can also try with LINQ to XML also.
var doc = XDocument.Load("yourfile.txt");
var nodes = from d in doc.Desendants("Customer")
select d;
foreach(var node in nodes)
{ // do stuff with node
}
the above code is not tested.