C#: How to save a dictionary to a text file? [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
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.

Related

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

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;

cast List object to new object extending 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
I'm trying to generate a custom xml doc, using this class:
[CollectionDataContract(Name="Applications", ItemName="Application")]
public class ApplicationNamesList : List<string> { }
The xml output im aming at should look like this
<Applications>
<Application>...</Application>
<Application>...</Application>
<Application>...</Application>
</Applications>
But once I have a List<string> object and try and cast this to ApplicationNamesList i get an InvalidCastException.
Is there something basic im not getting here?
A List<string> simply isn't a ApplicationNamesList. You would need to do something like this:
var result = new ApplicationNamesList();
result.AddRange(list);
With list being a List<string>.
Sometimes it helps to use real world examples:
Every Porsche ( => ApplicationNamesList) is a car ( => List<string>). But not every car is a Porsche.

Categories