I'm not sure that you really need to deal with JSON (and XML) directly. It depends on the origin of data. If you also generate data in the file/stream, you certainly should better use
Data Contract:
Using Data Contracts[
^],
DataContractSerializer Class (System.Runtime.Serialization)[
^],
DataContractJsonSerializer Class (System.Runtime.Serialization.Json)[
^].
Even if you don't generate the data but have to use some data generated by 3rd party, it's often possible to design the data contract matching the data model in question and the peculiarities of XML of JSON representation given. It's more likely to succeed in case of JSON.
And, finally, let's consider the cases when using data contract technology is not feasible for the model represented in XML, JSON, or both. What to do? Well, then, unfortunately, it's likely that you would have to develop data model-specific mapping code (in contrast to data contract, which is fully model-
agnostic).
As to JSON, please see my past answers:
haw to get data from Cloudant (json document)[
^],
How To Convert object type to C# class object type[
^],
how to conver multi level json data to C# Object?[
^].
(Sorry, in these answers, I also write about JSON in JavaScript, but I recommend to understand that, too, even if it is unrelated to your problem; first, it's good to understand the JavaScript nature of JSON, secondly, when JSON is used, one of the reasons is that JavaScript is always involved.)
To handle XML, .NET FCL offers different possibilities. This is my short overview:
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx. - Use the classes
System.Xml.XmlTextWriter
and System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that of XmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.
Good luck.
—SA