2011-12-04 66 views
0
 string json = "{"Animal":{"id":"123","verified":true}}" 

     XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); 

     returnXml = doc.ToString(); 

为什么“ReturnXml”返回以下文本“System.Xml.XmlDocument”而不是字符串格式的XML输出?JSON.NET XML to String


http://json.codeplex.com/

回答

5

要打印XML,你需要使用InnerXml

doc.InnerXml; 
+0

+1 - 比我好得多回答 –

1

的XmlDocument的ToString方法没有被设置为输出其中包含的XML的漂亮版本。

你最好的选择可能是这一点的XmlDocument转换成一个XDocument,因为支持该实际输出XML格式的ToString方法:

XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); 
XDocument linqXML = XDocument.Load(new XmlNodeReader(doc)); 
returnXML = linqXML.ToString(); 
+0

我该如何修复它,以便输出xml字符串? – 001

+0

你必须自己动手。检查了这一点http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument –