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):
or we have a JSON response (again: file, stream, ...) like:
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:
![[Image: ZQB26is.jpg]](https://i.imgur.com/ZQB26is.jpg)
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)
![[Image: 1JIpWvw.jpg]](https://i.imgur.com/1JIpWvw.jpg)
The full source:
Spoiler (Click to View)
Hope it helps someone and lets a bit clear now to handle XML/JSON data. ;)
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
<?xml version="1.0" standalone="yes"?>
<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
<publshInformation>
<Publish_Date>07/21/2016</Publish_Date>
<Record_Count>5393</Record_Count>
</publshInformation>
<sdnEntry>
<uid>36</uid>
</sdnEntry>
</sdnList>
or we have a JSON response (again: file, stream, ...) like:
PHP Code
<?php
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
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:
![[Image: ZQB26is.jpg]](https://i.imgur.com/ZQB26is.jpg)
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)
![[Image: 1JIpWvw.jpg]](https://i.imgur.com/1JIpWvw.jpg)
The full source:
Spoiler (Click to View)
Hope it helps someone and lets a bit clear now to handle XML/JSON data. ;)

