Saving ArrayList values to XML C# [closed] - c#

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.

Related

C# - Extract one *.jar file into another one [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 am trying to do my own Minecraft Launcher for Moded Minecraft. And I have a issue: I need to extract content of one *.jar file to another. I tried a lot of things and I am totally desperate.
Basicly, I need to do this in code.
As Andrew briefly mentioned, there are some good .NET libraries for manipulating zip files. I've found DotNetZip to be very useful, and there are lots of helpful worked examples here.
In answer to your question, if you just want to copy the contents of one *.jar file to another, keeping original content in the target file, please try the following. I'm using the .NET zip mentioned above (which also has a NuGet package!):
using (ZipFile sourceZipFile = ZipFile.Read("zip1.jar"))
using (ZipFile targetZipFile = ZipFile.Read("zip2.jar"))
{
foreach (var zipItem in sourceZipFile)
{
if (!targetZipFile.ContainsEntry(zipItem.FileName))
{
using (Stream stream = new MemoryStream())
{
// Write the contents of this zip item to a stream
zipItem.Extract(stream);
stream.Position = 0;
// Now use the contents of this stream to write to the target file
targetZipFile.AddEntry(zipItem.FileName, stream);
// Save the target file. We need to do this each time before we close
// the stream
targetZipFile.Save();
}
}
}
}
Hope this helps!

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();

Saving values from arraylist to txt 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
How to save everything from ArrayList to .txt file in c#, then on starting WPF application load it?
static void SaveArray()
{
ArrayList myArray = new ArrayList();
myArray.Add("First");
myArray.Add("Second");
myArray.Add("Third");
myArray.Add("and more");
StreamWriter sw= File.CreateText(#"C:\file.txt");
foreach (string item in myArray)
{
sw.WriteLine(item);
}
sw.Close();
}
and you shouldn't use arraylist not because its 2013 ,but because arraylist boxing each item in the array, where List store the type also.
this cost less in memory use.

Line Number of String [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
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;

C#: How to save a dictionary to a text 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
I've read up everywhere and cannot figure out how to save a dictionary to a .txt file. I know you can't just save the dictionary, you need to convert it to a string but I'm stuck at that. I have no idea how to convert it to a string, write it, then later read it back in and convert it from a string to a dictionary.
public static Dictionary<string, string> dict = new Dictionary<string, string>();
Try looking up serialization. You could serialize your Dictionary to binary and store that in a file. See the BinaryFormatter on msdn. Simply, you could:
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, dict);
There's also an XmlSerializer class.

Categories