[英]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.