[英]Serialize Xml to Json using type attribute in c#
我想在保持数字类型的json中序列化我的xml。
在我的XML下面:
<root type="number">42</root>
这是我的预期结果:
{
"root": 42
}
我正在尝试通过JsonConvert.SerializeXmlNode方法使用Newtonsoft库,但似乎不起作用:
我的密码:
XmlDocument dox1 = new XmlDocument();
string xx = "<root type=\"number\">42</root>";
dox1.LoadXml(xx);
string JsonContent = Newtonsoft.Json.JsonConvert.SerializeXmlNode(dox1, Newtonsoft.Json.Formatting.Indented, true);
//JsonResult json = Json(JsonContent);
return JsonContent;
结果:
{
"@type": "number",
"#text": "42"
}
你能帮助我吗?
您的XML与预期的JSON没有任何关系。 您可能会完全喜欢手动转换(我假设您的意思是42,否则解释逻辑为4):
void Main()
{
string xx = "<root type=\"number\">42</root>";
var value = new { orderType = (int)XElement.Parse(xx)};
var json = JsonConvert.SerializeObject(value);
Console.WriteLine(json);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.