将JSON转换为XML并保存XML

时间:2023-02-10 23:43:18

I am trying to convert some JSON to XML and then save it using JSON.NET in C# but i can't seem to get it.

我正在尝试将一些JSON转换为XML,然后使用JSON保存。在c#中,但我似乎不明白。

Here is what i have:

以下是我的资料:

using System.XML;
using Newtonsoft;

XmlDocument doc = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);
XmlTextWriter writer = new XmlTextWriter("json.xml", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);

1 个解决方案

#1


2  

I tested your code and it works totally fine for me. According to the documentation for DeserializeXmlNode this should definitely work:

我测试了你的代码,它对我来说完全没问题。根据DeserializeXmlNode的文档,这肯定是可行的:

// { "?xml": { "@version": "1.0", "@standalone": "no" }, "root": { "person": [ { "@id": "1", "name": "Alan", "url": "http://www.google.com" }, { "@id": "2", "name": "Louis", "url": "http://www.yahoo.com" } ] } }
string json = "{ \"?xml\": { \"@version\": \"1.0\", \"@standalone\": \"no\" }, \"root\": { \"person\": [ { \"@id\": \"1\", \"name\": \"Alan\", \"url\": \"http://www.google.com\" }, { \"@id\": \"2\", \"name\": \"Louis\", \"url\": \"http://www.yahoo.com\" } ] } }";

System.Xml.XmlDocument xmlDocument = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);
System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter("json.xml", null);
xmlTextWriter.Formatting = System.Xml.Formatting.Indented;
xmlDocument.Save(xmlTextWriter);

//<?xml version="1.0" standalone="no"?>
//<root>
//  <person id="1">
//    <name>Alan</name>
//    <url>http://www.google.com</url>
//  </person>
//  <person id="2">
//    <name>Louis</name>
//    <url>http://www.yahoo.com</url>
//  </person>
//</root>

Test your method with the JSON string above, to verify if it works. I would say you are having a problem with your JSON not being valid.

使用上面的JSON字符串测试您的方法,以验证它是否有效。我想说的是,您的JSON存在问题。

You can validate your JSON for example here:

可以在这里验证JSON:

#1


2  

I tested your code and it works totally fine for me. According to the documentation for DeserializeXmlNode this should definitely work:

我测试了你的代码,它对我来说完全没问题。根据DeserializeXmlNode的文档,这肯定是可行的:

// { "?xml": { "@version": "1.0", "@standalone": "no" }, "root": { "person": [ { "@id": "1", "name": "Alan", "url": "http://www.google.com" }, { "@id": "2", "name": "Louis", "url": "http://www.yahoo.com" } ] } }
string json = "{ \"?xml\": { \"@version\": \"1.0\", \"@standalone\": \"no\" }, \"root\": { \"person\": [ { \"@id\": \"1\", \"name\": \"Alan\", \"url\": \"http://www.google.com\" }, { \"@id\": \"2\", \"name\": \"Louis\", \"url\": \"http://www.yahoo.com\" } ] } }";

System.Xml.XmlDocument xmlDocument = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);
System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter("json.xml", null);
xmlTextWriter.Formatting = System.Xml.Formatting.Indented;
xmlDocument.Save(xmlTextWriter);

//<?xml version="1.0" standalone="no"?>
//<root>
//  <person id="1">
//    <name>Alan</name>
//    <url>http://www.google.com</url>
//  </person>
//  <person id="2">
//    <name>Louis</name>
//    <url>http://www.yahoo.com</url>
//  </person>
//</root>

Test your method with the JSON string above, to verify if it works. I would say you are having a problem with your JSON not being valid.

使用上面的JSON字符串测试您的方法,以验证它是否有效。我想说的是,您的JSON存在问题。

You can validate your JSON for example here:

可以在这里验证JSON: