How to convert a specified node to an array json.net when XML is converted to Json format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.