Save Object To File. From here you can serialize an object to file in two way. Solution-1: Serialize JSON to a string and then write string to a file string json = JsonConvert.SerializeObject(myObj); File.WriteAllText(@'c:myObj.json', json); Solution-2: Serialize JSON directly to a file.
Active1 year ago
This question already has an answer here:
- Convert XML String to Object 13 answers
I have this XML,How should i model the Class so i will be able to deserialize it using
user829174user829174XmlSerializer
object?2,4301313 gold badges5858 silver badges9999 bronze badges
marked as duplicate by Michael Freidgeim, Mark Rotteveel, EdChum, Nkosi, P.P.Dec 31 '16 at 16:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2 Answers
Your classes should look like this
Here is my testcode.
If you want to read a text file you should load the file into a FileStreamand deserialize this.
dknaackdknaack49.1k2020 gold badges124124 silver badges170170 bronze badges
The comments above are correct. You're missing the decorators. If you want a generic deserializer you can use this.
Then you'd call it like this:
Jay JacobsJay Jacobs71911 gold badge1111 silver badges2222 bronze badges
Not the answer you're looking for? Browse other questions tagged c#xml-deserialization or ask your own question.
Active2 years, 9 months ago
I'm using NewtonSoft for handling json in my wpf application. I've got a customer that can be saved to a txt file (no database involved). I'm doing that like this:
The result looks like this:
Then I'm trying to get all customers like this:
But I receive this error (can't copy the error):
C# Serialize Object To Json
Already tried to store it like a jArray but that's not working. How do I get this to work?
Any help is going to be appreciated. :)
JenssenJenssen44111 gold badge1414 silver badges3535 bronze badges
2 Answers
MahdiMahdi2,34022 gold badges1717 silver badges2727 bronze badges
Your problem is that you try to get a List of Customer from your file while you are saving only one customer.
If you want store multiple customers in your file you have to create a JArray and add your customer into it :
Then, just save the JArray of customer :
You'll probably have to adapt this code to your own needs.
I try my best to write correct english, but be free to correct me.
csblocsblo