![]() |
|
[.NET] 101 XML/JSON - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] 101 XML/JSON (/showthread.php?tid=5353651) |
101 XML/JSON - Zilog80 - Jul 27, 2016 I decided to write a 101 XML/JSON tutorial I have seen already several buddies around HF having troubles using XML with LoadXml, XPath, ... My how-to does not make use for LoadXml. It use serialization and works fine also with JSON with out re-writting the whole code each time we need "load" an XML with different tags/properties, because it use Generics. Also i'll show you a really nice Visual Studio feature introduced some time ago (think its available since VS 2012) and maybe some people don't know: Paste JSON or XML as VB.NET or C# class. Part one. How to create 'our' XML or JSON class: Visual Studio Paste Special Suppose we have a XML response (file, stream or what ever you want) like @antibiotic1 post (hackforums.net/showthread.php?tid=5353238): PHP Code or we have a JSON response (again: file, stream, ...) like: PHP Code We can "paste" that XML and/or JSON into Visual Studio and Visual Studio generates it self the properly classes we need. [img]http://i.stack.imgur.com/N1KBR.jpg[/img] Pro-Tip #1: Paste Special option menu it only shows up when you have a .cs or .vb file open. If you are on Form dessign view will not show up Pro-Tip #2: Paste Special >> Paste XML as Classes only shows if your projet target is .NET 4.5 or above. So, copy @antibiotic1 XML and then in Visual Studio, with .cs or .vb file open select Paste XML as Classes. And voilĂ , Visual Studio generate all this code automatically: Spoiler (Click to View) Part two. Deserialization or not deserialization-that is the question As i said before, here we are using serialization stuff instead LoadXml methods. We use generics to hande any XML/JSON structure. Also we use standard .NET Framework namespaces, we not requiered any NuGet for JSON, like JSON.NET or 'special' libraries. I created a couple tiny classes for manage XML and JSON. Basically: Serialize data objet to file and Deserialize file to object You can put this classes on Helpers folder to c&p on every project you need to use. Part three. Show me a demo! Once we get classes pasted AS we can do this: ![]() We have a XML file (j:\temp\data.xml). The file content is @antibiotic1 sample posted above. As you can see, with only 1 line code we have an sdnList object called myData (sdnList class were generated with Paste Special) We can query any method from myData, change, or whatever. And also, with 1 line code we can save that changes to a file (XML or JSON) ![]() The full source: Spoiler (Click to View) Hope it helps someone and lets a bit clear now to handle XML/JSON data. ;) * - HFDone - Jul 27, 2016 theres already loads of examples for it if just ppl use the search button |