Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have json array data and converted to xml
Sample json data:

public class jsonInput
   {
       public List<SyncActivity> General { get; set; }


       public List<SyncActivity> RA { get; set; }


       public List<SyncActivity> WA { get; set; }
   }


xml converstion below here

string objActivityDetails = JsonConvert.SerializeObject(Activities);
            XmlDocument xmlObject = JsonConvert.DeserializeXmlNode("{\"root\":" + objDetails.ToString() + "}");

after xml converstion i got the output

<root>
 
<RA>

<MemberId>12333354354</MemberId>

<UserId>4t34534534</UserId>

<CreatedBy>45435345</CreatedBy>

<UpdatedBy>0</UpdatedBy>

<UpdatedDate/>

</RA>
<pre><RA>

<MemberId>12333354354</MemberId>

<UserId>4t34534534</UserId>

<CreatedBy>45435345</CreatedBy>

<UpdatedBy>0</UpdatedBy>

<UpdatedDate/>

</RA>


<WA>

<MemberId>12333354354</MemberId>

<UserId>4t34534534</UserId>

<CreatedBy>45435345</CreatedBy>

<UpdatedBy>0</UpdatedBy>

<UpdatedDate/>

</WA>
<pre><WA>

<MemberId>12333354354</MemberId>

<UserId>4t34534534</UserId>

<CreatedBy>45435345</CreatedBy>

<UpdatedBy>0</UpdatedBy>

<UpdatedDate/>

</WA>





But i need below format :

<roo>
<WA MemberId="12333354354" UserId="4t34534534" CreatedBy="45435345" />
<WA MemberId="12333354354" UserId="4t34534534" CreatedBy="45435345" />

<RA MemberId="12333354354" UserId="4t34534534" CreatedBy="45435345" />
<RA MemberId="12333354354" UserId="4t34534534" CreatedBy="45435345" />
</root>


please let me know you're valuable feedback.

What I have tried:

i tried JSON serialization. but returns child nodes. i expected child nodes as attributes
Posted
Updated 6-Jun-18 5:45am
Comments
Mehdi Gholam 5-Jun-18 9:19am    
Why (attributes and elements are the same)?
Akula santhosh kumar 5-Jun-18 9:47am    
I will pass the XML data to SP. I have heavy data to transfer SP

1 solution

If you want to stick with this inefficient approach, you'll need the JSON property names to be prefixed with the "@" character:
C#
public class SyncActivity
{
    [JsonProperty("@MemberId")]
    public string MemberId { get; set; }
    
    [JsonProperty("@UserId")]
    public string UserId { get; set; }
    
    [JsonProperty("@CreatedBy")]
    public int CreatedBy { get; set; }
    
    [JsonProperty("@UpdatedBy")]
    public int UpdatedBy { get; set; }
    
    [JsonProperty("@UpdatedDate")]
    public DateTime UpdatedDate { get; set; }
}
Converting between JSON and XML[^]

But converting your object to JSON just to convert it back to XML is horribly inefficient. It would be much simpler to convert the object directly to XML:
C#
XDocument document = new XDocument(
    new XElement("root",
        Activities.General.Select(a => new XElement("General",
            new XAttribute("MemberId", a.MemberId),
            new XAttribute("UserId", a.UserId),
            new XAttribute("CreatedBy", a.CreatedBy),
            new XAttribute("UpdatedBy", a.UpdatedBy),
            new XAttribute("UpdatedDate", a.UpdatedDate)
        )),
        Activities.RA.Select(a => new XElement("RA",
            new XAttribute("MemberId", a.MemberId),
            new XAttribute("UserId", a.UserId),
            new XAttribute("CreatedBy", a.CreatedBy),
            new XAttribute("UpdatedBy", a.UpdatedBy),
            new XAttribute("UpdatedDate", a.UpdatedDate)
        )),
        Activities.WA.Select(a => new XElement("WA",
            new XAttribute("MemberId", a.MemberId),
            new XAttribute("UserId", a.UserId),
            new XAttribute("CreatedBy", a.CreatedBy),
            new XAttribute("UpdatedBy", a.UpdatedBy),
            new XAttribute("UpdatedDate", a.UpdatedDate)
        ))
    )
);
This will also give you complete control over the format of the generated XML document.
 
Share this answer
 
Comments
Maciej Los 7-Jun-18 4:59am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900