First, about this article
This article has undertaken Previous Blog post the content. In the previous blog, a JSON-formatted file was written to a corresponding XML file through the function Writejsontoxml. The code for the function recoverjsonfromxml is given in this article , which restores the XML to JSON-formatted text.
The environment for running the code in this article is the same as in the previous blog.
The contents of the XML document to be read are as follows:
<?xml version= "1.0" encoding= "gb2312"?><!--This XML document stores a JSON-formatted message--><root> <name type= "String "Value=" Tsybius "/> <age type=" Integer "value=" All "/> <sex_is_male type=" Boolean "value=" True "/> <p Artner type= "Object" > <partner_name type= "String" value= "Galatea"/> <partner_age type= "Integer" value= " "/> <partner_sex_is_male type=" Boolean "value=" False "/> </Partner> <achievement type=" Array "Va lue0= "Ach1" value1= "Ach2" value2= "Ach3"/></root>
Second, function recoverjsonfromxml
' <summary> ' storing XML-formatted content in a JSON-formatted string ' </summary> ' <param name= "Address" > XML file addresses to read </param> "' <remarks></remarks>public shared function recoverjsonfromxml (address as string) As String Dim Xmldoc = new xmldocument xmldoc.load (address) dim xmlroot as xmlnode = xmldoc.selectsinglenode ("Root") dim jtemp as jobject = writetojson (XmlRoot) Return Jsonconvert.serializeobject (jtemp, newtonsoft.json.formatting.indented) end function " < Summary> ' ' </summary> ' <param name= ' xnode ' for reading a jobject type data in XmlNode type ' ' >xml node type data </param> ' <returns>jobject type data </returns> ' <remarks></ remarks>private shared Function writetojson (Xnode as xmlnode) As JObject Dim Jobj as new jobject for each obj as object in xnode.childnodes if obj. gettype.tostring = "System.Xml.XmlElement" Then ' read to XML node dim xElmt As XmlElement = obj select case xelmt.getattribute ("Type") Case "string" "string variable jobj. ADD (Xelmt.name, xelmt.getattribute ("Value")) Case "Integer" ' Integer variables jobj. ADD (Xelmt.name, integer.parse (Xelmt.getattribute ("Value"))) Case "float" "floating-point variable Jobj. ADD (Xelmt.name, double.parse (Xelmt.getattribute ("Value"))) Case "Boolean" "Boolean variable jobj. ADD (Xelmt.name, iif (Xelmt.getattribute ("Value") = "True", new jvalue (True), new jvalue (False))) Case "Array" "JSON array Dim jarr As jarray = new jarray () for i as integer = 0 to xElmt.Attributes.Count - 2 jarr. ADD (New jvalue (Xelmt.getattribute ("Value" & i.tostring)) next jobj. ADD (Xelmt.name, jarr) Case "Object" "JSON object jobj. Add (Xelmt.name, writetojson (obj)) End select elseif obj. gettype.tostring = "System.Xml.XmlComment" Then ' Read to comment continue For Else ' Other conditions continue for end if next return jobjend function
Iii. Invocation of the example
Main code
Sub Main () Dim Sjson as String = Jsonxmlhelper.recoverjsonfromxml ("X.xml") Console.WriteLine (Sjson) Console.readl INE () End Sub
Iv. Results of operation
END