下面是我的stringify JSON数据
"{\"TimeSheet\":{\"TimeSheetDetail\":[{\"TimeSheetDetailsDate\":\"Mon May 26 00:00:00 UTC+0530 2014\",\"Hours\":\"732\"},{\"TimeSheetDetailsDate\":\"Tue May 27 00:00:00 UTC+0530 2014\",\"Hours\":\"732\"},{\"TimeSheetDetailsDate\":\"Wed May 28 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Thu May 29 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Fri May 30 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Sat May 31 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Sun Jun 1 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"}]}}"
我使用下面的代码将JSON数据修改为XML
var doc = JsonConvert.DeserializeXmlNode("{\"root\":[" + timeSheetJSon + "]}", "root");
XDocument.Parse(doc.InnerXml);
它返回<root><root>{"TimeSheet":{"TimeSheetDetail":[{"TimeSheetDetailsDate":"Mon May 26 00:00:00 UTC+0530 2014","Hours":"732"},{"TimeSheetDetailsDate":"Tue May 27 00:00:00 UTC+0530 2014","Hours":"732"},{"TimeSheetDetailsDate":"Wed May 28 00:00:00 UTC+0530 2014","Hours":"1281"},{"TimeSheetDetailsDate":"Thu May 29 00:00:00 UTC+0530 2014","Hours":"1281"},{"TimeSheetDetailsDate":"Fri May 30 00:00:00 UTC+0530 2014","Hours":"1281"},{"TimeSheetDetailsDate":"Sat May 31 00:00:00 UTC+0530 2014","Hours":"1281"},{"TimeSheetDetailsDate":"Sun Jun 1 00:00:00 UTC+0530 2014","Hours":"1281"}]}}</root></root>
但我需要的是XML格式。 如果有任何解决方案,那将是很有帮助的。
答案 0 :(得分:3)
[TestMethod]
public void ConvertJsonToXml()
{
string json = "{\"TimeSheet\":{\"TimeSheetDetail\":[{\"TimeSheetDetailsDate\":\"Mon May 26 00:00:00 UTC+0530 2014\",\"Hours\":\"732\"},{\"TimeSheetDetailsDate\":\"Tue May 27 00:00:00 UTC+0530 2014\",\"Hours\":\"732\"},{\"TimeSheetDetailsDate\":\"Wed May 28 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Thu May 29 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Fri May 30 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Sat May 31 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"},{\"TimeSheetDetailsDate\":\"Sun Jun 1 00:00:00 UTC+0530 2014\",\"Hours\":\"1281\"}]}}";
var xdoc = JsonConvert.DeserializeXNode(json);
var xml = xdoc.ToString();
Assert.IsNotNull(xml);
}
xml结果:
<TimeSheet>
<TimeSheetDetail>
<TimeSheetDetailsDate>Mon May 26 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>732</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Tue May 27 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>732</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Wed May 28 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>1281</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Thu May 29 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>1281</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Fri May 30 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>1281</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Sat May 31 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>1281</Hours>
</TimeSheetDetail>
<TimeSheetDetail>
<TimeSheetDetailsDate>Sun Jun 1 00:00:00 UTC+0530 2014</TimeSheetDetailsDate>
<Hours>1281</Hours>
</TimeSheetDetail>
</TimeSheet>