When using Json.NET to convert XML into JSON, if the XML has only a single node, but the JSON requirement is an array form [],
Jsonconvert.serializexmlnode
does not automatically identify
Examples are as follows:
Recordarray requirements are array format
<root><record></record><recordarray><a>1</a><b>2</b></ Recordarray></root>
The converted JSON does not meet the requirements
{" root": {" Record": "", "Recordarray": { "a": "1", "B": "2" }}}
Solutions
Find out how easy it is to check the information
The XML root node needs to be prefixed with attributes
xmlns:json= ' Http://james.newtonking.com/projects/json '
You need to convert an array of nodes plus attributes
Json:array= ' true '
As shown below
<root xmlns:json= ' Http://james.newtonking.com/projects/json ' >
<Record>
</Record>
<recordarray json:array= ' true ' >
<a>1</a>
<b>2</b>
</RecordArray>
</root>
The converted JSON can meet the requirements
{" root": { "Record": "", "Recordarray": [ { "a": "1", "B": "2" } ]} }
XML Add attribute:
When adding a property, you can create a property for the node directly at the time of creation of the xmlelment, either through XmlElement setattribute, or by creating
A XmlAttribute instance: XmlAttribute xmlarr=xmldocument.createattribute ("attribute value"), and then through XmlNode's
Attributes.Add (Xmlarribute) to add
You can also replace string characters (only for XML that does not have duplicate nodes)
Xmlinfo = Xmlinfo.replace ("<recordarray>", "<recordarray json:array= ' true ' >");
Reference: https://q.cnblogs.com/q/41823/
https://stackoverflow.com/questions/21382879/ xml-to-json-using-newtonsoft-issue-when-loop-over-array-of-one-element/21383423#21383423
Https://www.newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray.htm
How to convert a specified node to an array json.net when XML is converted to Json format