amit jha
amit jha

Reputation: 416

@ symbol present before attributes of XML when converting to JSON

I am converting the following XML to JSON:

<NODE1>
  <NODE2>200</NODE2>
    <NODE3>Got the contents</NODE3>
      <NODE4 INDEX="1" SIZE="2" TOTALPAGES="1" TOTAL="2">
      <NODE5 ID="94086" TITLE="Bo Aung Din Lo Lu Ky" />
      <NODE5 ID="94087" TITLE="Bo Aung Din Lo Lu Ky" />
    </NODE4>
</NODE1>

using the following code:

XmlDocument doc = new XmlDocument();
doc.Load("C:\\1.xml");
string jsonText = JsonConvert.SerializeXmlNode(doc);

The problem that I am getting is that @ symbol is coming before attributes of XML, i.e., @ID instead of ID.

Is this right? Or a bug in the library?

Upvotes: 1

Views: 571

Answers (1)

Richard
Richard

Reputation: 109100

It is right.

It is documented to have that behaviour:

Attributes are prefixed with an @ and should be at the start of the object.

(From https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm)

I assume this is because JSON just has properties for structure, whereas XML has elements and attributes (and their names could overlap: the same element having a child element with the same name as an attribute). By prefixing the names the output JSON retains the distinction.

Upvotes: 2

Related Questions