2011-01-08 6 views
2

일부 JSON을 XML로 변환하려고 시도하고 C#에서 JSON.NET을 사용하여 저장하려고합니다. 그러나 얻을 수없는 것 같습니다.JSON을 XML로 변환하고 XML을 저장합니다.

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); 
+0

? 오류가 있습니까? 예외? – Oded

+0

한 가지 예외가 있습니다. 'Newtonsoft.Json.JsonSerializationException '유형의 첫 번째 예외는 Newtonsoft.Json.Net35.dll'에서 발생했습니다. – user556396

+0

즉,'Json.NET'에 의해 잡히고 처리되었습니다. 아직도 작동하지 않는 것을 설명하지 않았습니다. – Oded

답변

2

내가 코드를 테스트하고 나를 위해 완전히 잘 작동 : 여기

내가 가진 것입니다. 이것은 확실히해야 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> 

가 작동하는지 확인하기 위해, 위의 JSON 문자열로 당신의 방법을 테스트합니다. 귀하의 JSON에 문제가있는 것 같아요.

여기 예를 들어 당신의 JSON 유효성을 검사 할 수 있습니다 : 무엇을 작동하지

관련 문제