用json.net将json转换为xml

时间:2011-05-12 11:54:54

标签: .net xml json.net

我想找一个使用json.net将json响应转换为xml的例子。我不确定我是否应该使用LINQ to JSON或XmlNodeConverter或者是什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

http://james.newtonking.com/projects/json/help/

“在JSON和XML之间转换”一节应该会有所帮助。它有一些简单的例子。

答案 1 :(得分:0)

我没有使用过json.net,但是如果你有一个clr集合,你可以使用linq为你生成xml。

例如:

var xml = 
    new XElement("people",
    from x in personCollection
    orderby x.LastName
    select new XElement("person",
        new XAttribute("personId", x.PersonId),
        new XElement("firstName", x.FirstName),
        new XElement("lastName", x.LastName)))
    );

一个例子就是:

<people>
    <person ID="1">
        <firstName>first-name-1</firstName>
        <lastName>last-name-1</lastName>
    </person>
    <person ID="2">
        <firstName>first-name-2</firstName>
        <lastName>last-name-2</lastName>
    </person>
    <person ID="1">
        <firstName>first-name-3</name>
        <lastName>last-name-3</lastName>
    </person>
</people>