
| home | AJAX (6) || C#.NET (5) || Coldfusion Development (16) || DHTML (14) || Flash Development (19) || jQuery (4) || MSSQL (2) || UNIX (10) |
| 4.22.08 | Convert Single Level “XML” To Native C#.NET Object Via Json And Regular Expressions |
So we have a 3rd party API that’s returning “XML” in as though it was a DTD-less WDDX or XML-RPC format. We need to convert this “XML” into native C#.NET objects. The solution is a bit on the “Perl Hackish” side of things - but the simplest and fastest method I could come up with was to parse the “XML” into a JSON object then into a Native C#.NET object. The JSON library of choice is LitJson
Here’s an example of our “XML”:
Download this code: xmlToJsonToObject.xml
Yes - we have the problem of this not even being close to valid XML which completely rules out use of the System.Xml functionality.
Now the C# code to take the “XML” string and parse it into C#.NET object via LitJson’s functionality:
Download this code: xmlToJsonToObject.cs
The overset.com.CSMadness.xmlToJsonToObject() method will convert the
<>114848000000</><amount>0.00</amount><avs_addr>""YARR</avs_addr>
to
{"amount":"0.00","avs_addr":"\"YARR"} while stripping out the completely invalid <> node. This will also consider all node values to be a string.
No comments