Json.NET - 将JSON转换为XML并删除XML版本,编码?

时间:2011-05-24 12:31:32

标签: c# xml json json.net xml-deserialization

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

为什么我使用“DeserializeXmlNode”并将我的JSON转换为XML文档 然后将我的XML文档转换为像这样的字符串

   string strXML = "";
   StringWriter writer = new StringWriter();
   xmlDoc.Save(writer);
   strXML = writer.ToString();

它包括

<?xml version="1.0" encoding="utf-16"?>

我没有添加此内容,如何将其删除?

2 个答案:

答案 0 :(得分:4)

没有该行的XML不是有效的XML file

该行称为 XML声明

例如,查看OData Netflix Catalog Titles XML,您能看到第一行吗?

  

http://odata.netflix.com/Catalog/Titles

答案 1 :(得分:3)

将XmlWriter与StringBuilder一起使用,而不是使用StringWriter

 var strXML = "";
 var writer = new StringBuilder();   
 var settings = new System.Xml.XmlWriterSettings() { OmitXmlDeclaration = true};
 var xmlWriter = System.Xml.XmlWriter.Create(strXML, settings);   
 xmlDoc.Save(xmlWriter);
 strXML = writer.ToString();