Line Number of String [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
StreamReader reader = new StreamReader("C:\\ABC\\XYZ.txt");
I am reading a file using streamreader, the file is a HL7 file
MSH|^~\&|ABC|000|ABC|ABC|0000||ABC|000|A|00
PID|1|000|||ABC||000|A||||||||||
PV1|1|O||||||||||||||||||||||||||||||||||||||||||
OBR|1|||00||00|00|||||||||||ABC|00|0|0||||A|||||00||ABC|7ABC||ABC
OBX|1|ABC|ABC|1|SGVsbG8=
I need to find the line number of OBX, the file has character delimiters at the end of each line e.g. MSH|^~\&|ABC|000|ABC|ABC|0000||ABC|000|A|00*CR*LF
The reason I need this is that I need to get the Base64 inside the OBX field, and write it out as a file. my reader will always be a stream, I cannot use the file stream. The above code was an example, the following implementation is to be made in BizTalk and the file I will reading will be stream because that's how BizTalk allows me to access the information in of my file.

var lineNum = File.ReadLines(fname)
.Select((s, line) => new { s, line })
.First(x => x.s.StartsWith("OBX|"))
.line;

Related

Append multiple text files into single file and Rename the file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hello I have multiple text files in one location and they have names as shown below:-
A_L_PRTD_021345.txt , A_L_PRTD_432124 and so on...
I want to append the contents into single file and want to rename the single file as L_PRTD_Currentdate>.txt
How is it possible?
Can you please show some code for it?
First read each file using File.ReadAllLines(fileName) ;
Then create a single file and append the contents of other files in it
string[] contents1 = File.ReadAllLines(fileName1) ;
string[] contents2 = File.ReadAllLines(fileName2) ;
File.Create(newFileNameHere) ;
File.WriteAllLines(newFileNameHere, contents1) ;
File.AppendAllLines(newFileNameHere, contents2) ;
and so on ...
Check the following documentation of the functions, if you need help:-
http://msdn.microsoft.com/en-us/library/92e05ft3(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/dd383691(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx

How to know the page break in Excel using C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I´m using C# to create and format a Excel spreadsheet, so I need to format (merge cells, change the font, etc) until the final of the first page. How can I know the final line of the page in the Excel spreadsheet?
The following code will give you the last row number before every horizontal page break in an Excel worksheet:
foreach (Excel.HPageBreak pageBreak in worksheet.HPageBreaks)
{
int row = pageBreak.Location.Row - 1;
// ...
}

Saving ArrayList values to XML C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have ArrayList nabídka, I have to save everything from this ArrayList to XML file using stream writer and on starting application load the XML file to ArrayList with stream reader. Does anybody know how to do it? (Yes, I know that I shouldn not use ArrayList, but i must complete this project with it.)
Heres an example of how it might be implemented
ArrayList sampleList = new ArrayList();
sampleList.Add(" ");
//Add your elements
//StreamWriter initialized with append mode
StreamWriter streamwriter = new StreamWriter(" INSERT PATH OF XML HERE ", true);
for (int i = 0; i < sampleList.Count; i++)
{
//Elements are written into the file here, remember not to forget the xml structure
streamwriter.WriteLine(sampleList[i]);
}
//You have to close the streamwriter or you have to flush to make sure the text is saved
streamwriter.Close();
I hope I could help you
Also possible:
var xmlSerializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(YourType) });
But for serializing it would be better to have a List<t>.
The benefit of serializing is the easy read in and that you even can share whole objects easily between languages/applications.

how to use StreamReader in C# to read only entries over a specific length to a list? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
when using StreamReader in C# to load a txt file into a list, i assume that using a simple "If" the string's length is over a particular length, it will add it to the list. can anyone provide C# code for this? this IS homework, but it's NOT a C# class. the instructor would gladly provide this if i asked this specifically. thx.
the txt file is a dictionary of ~280,000 words, one per line. very simple move to turn into a list, but i'm wondering about getting words at least 2 characters long.
Just use LINQ to give you a subset.
List<string> lines = File.ReadLines(filename)
.Where(l => l.Length > specifiedWordLength)
.ToList();

write values in a .txt file in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I m new to programming and i m looking for source code which can write and add values to a text file using C# programming. The code should ask the user to input values in the console window. the values are :-
Enter Name, Enter account no., Enter Balance
the output should get stored in the text file in the following format :-
Mark,10001,230000
Steve,10002,32987
Bruce,10003,23454
William,10004,23454
I would appreciate some help.
string name;
int number1,number2;
// Read name, number1, number2 from Console
//.....
//.....
//Saving in a file:
string outputFileName = #"c:\myfile.txt"
StreamWriter sw = new StreamWriter(outputFileName);
sw.WriteLine(string.Format("{0},{1},{2}",name,number1,number2));
sw.Close();

Categories