【问题标题】:Convert a json text with attributes to xml将带有属性的json文本转换为xml
【发布时间】:2016-11-01 12:40:05
【问题描述】:

我有以下 json,它是从 XML 转换而来的。

{  
    "@number":"0",
    "Field":[  
        {  
            "@key":"status",
            "a":"1"
        }
    ]
}

当我使用 JsonConvert.DeserializeXmlNode() 函数将其转换回 XML 时,它给了我以下异常。

System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Converters.XmlDocumentWrapper' to type 'Newtonsoft.Json.Converters.IXmlElement'.

我该怎么做?

【问题讨论】:

    标签: c# json xml


    【解决方案1】:
    var result = JsonConvert.DeserializeXNode(json, "Root");
    
    //output
    <Root number="0">
     <Field key="status">
       <a>1</a>
     </Field>
    </Root>
    

    这工作没有问题。请注意,@number@key 将是特定节点的属性!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 2017-08-28
      • 2016-08-09
      • 2017-04-24
      • 1970-01-01
      • 2011-06-18
      • 2016-10-26
      • 2019-01-04
      相关资源
      最近更新 更多