如何使用C#中的属性将json转换为XML [英] How to convert json to XML with attributes in C#

查看:139
本文介绍了如何使用C#中的属性将json转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有json数组数据并转换为xml

示例json数据:



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下面的转换





xml converstion below here

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


$ x $ b xml转换后我得到了输出




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>





请告诉我您的宝贵意见。



我尝试了什么:



i尝试过JSON序列化。但返回子节点。我希望子节点作为属性



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

推荐答案

如果你想坚持这种低效的方法,你需要JSON属性名称以@字符作为前缀:

If you want to stick with this inefficient approach, you'll need the JSON property names to be prefixed with the "@" character:
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; }
}

在JSON和XML之间转换 [< a href =https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htmtarget =_ blanktitle =New Window> ^ ]



但是将对象转换为JSON只是为了将其转换回XML是非常低效的。将对象直接转换为XML要简单得多:

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:

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)
        ))
    )
);

这也可以让您完全控制生成的XML文档的格式。

This will also give you complete control over the format of the generated XML document.


这篇关于如何使用C#中的属性将json转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆