I am new for Json and I have a simple problem.
I am trying to convert json file to xml file with c#. But it throw an exception.
The Code is ;
private void TakeXML() { string json = ReadText(); XmlDocument doc = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json); XmlTextWriter writer = new XmlTextWriter("json.xml", null); writer.Formatting = Formatting.Indented; doc.Save(writer); }The ReadText function is;
private string ReadText() { FileStream fs = new FileStream(@"C:\Users\Sinan\Desktop\bina.json", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string json; try { json = sr.ReadToEnd(); return json; } catch (Exception) { return null; } finally { sr.Close(); fs.Dispose(); } }for XmlDocument doc = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json); line, it said that;
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName."
I am searching to solve this problem but haven't found it. f you help me in this regard, I will be glad. Thank you.
我是新的JSON和我有一个简单的问题。
我想转换成JSON文件与C # XML文件。但它抛出一个异常。
该代码是;
private void TakeXML() { string json = ReadText(); XmlDocument doc = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json); XmlTextWriter writer = new XmlTextWriter("json.xml", null); writer.Formatting = Formatting.Indented; doc.Save(writer); }READTEXT函数;
private string ReadText() { FileStream fs = new FileStream(@"C:\Users\Sinan\Desktop\bina.json", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string json; try { json = sr.ReadToEnd(); return json; } catch (Exception) { return null; } finally { sr.Close(); fs.Dispose(); } }为XmlDocument doc = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);线的,它说;
“JSON根对象具有多个属性。根对象必须以创造一个有效的XML文档有一个单一的财产。考虑一个deserializerootelementname化。”
我寻找解决这一问题,但没找到。如果你在这方面帮助我,我会很高兴的。谢谢你