There is no way that Json.NET will insert a hardcoded comment when serializing an XmlDocument -- there simply isn't any logic to do this in XmlNodeConverter. The only time XmlNodeConverter will write a comment is if an XML node of type XmlNodeType.Comment was actually encountered in the XML DOM hierarchy, at around line 1502 in the source code:
case XmlNodeType.Comment: if (writePropertyName) { writer.WriteComment(node.Value); } break;
Thus there are only a few ways such a comment string could have gotten added to your JSON output, including:
There might actually be comments in your XML, added by the Zillow API or by your own app somewhere in its code stack. For instance, given the following XML:
<?xml version="1.0" encoding="utf-8" ?> <root> <childNode> <innerChildNode>Some Text</innerChildNode> </childNode> <!-- H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda --> </root> <!-- H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda -->
Json.NET will generate the following JSON:
{ "?xml": { "@version": "1.0", "@encoding": "utf-8" }, "root": { "childNode": { "innerChildNode": "Some Text" }/* H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda */ }/* H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda */ }
Somewhere in your code stack, your app might have installed its own version of XmlNodeConverter in JsonConvert.DefaultSettings that inserts comments into the output. For instance, given the following global converter:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = { new FixedXmlNodeConverter() }, }; public class FixedXmlNodeConverter : Newtonsoft.Json.Converters.XmlNodeConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { base.WriteJson(writer, value, serializer); writer.WriteComment("Global Comment: H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda"); } }
The JSON generated will contain extra comments:
{ "?xml": { "@version": "1.0", "@encoding": "utf-8" }, "root": { "childNode": { "innerChildNode": "Some Text" }/* H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda */ }/* H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda */ }/*Global Comment: H:011 T:134ms S:1206 R:Tue May 02 07:59:03 PDT 2017 B:5.0.42642-master.3acb9f9~hotfix_pre.b72ccda*/
You might be using your own custom build of Json.NET with a customized version of XmlNodeConverter.
Sample fiddle demonstrating the first two possibilities.
If you actually have comment nodes in your XML, you can strip them by following one of the suggestions in How to remove all comment tags from XmlDocument or any number of similar questions. If you have a global converter installed, you can supersede it by manually allocating an XmlNodeConverter and passing it to JsonConvert.SerializeObject().
To handle these two cases, your GetJson() might become:
public String GetJson(XmlDocument xml) { XmlNodeList list = xml.SelectNodes("//comment()"); foreach(XmlNode node in list) { node.ParentNode.RemoveChild(node); } var converter = new Newtonsoft.Json.Converters.XmlNodeConverter(); // Use Newtonsoft.Json.Formatting.None in your production code return Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.Indented, converter); }
Sample fiddle.
If you are using your own custom build of Json.NET, you would need to investigate and fix why this is happening.
If for whatever reason you cannot modify the incoming XmlDocument (or cannot fix your custom build of Json.NET) you could subclass JsonTextWriter and override WriteComment (and possibly WriteCommentAsync though that's not needed here) and make them do nothing:
public class NoCommentJsonTextWriter : JsonTextWriter { public NoCommentJsonTextWriter(TextWriter writer) : base(writer) { } public override void WriteComment(string text) { } }
Then use it as follows:
public String GetJson(XmlDocument xml) { var sb = new StringBuilder(); using (var textWriter = new StringWriter(sb)) // Use Newtonsoft.Json.Formatting.None in your production code using (var writer = new NoCommentJsonTextWriter(textWriter) { Formatting = Newtonsoft.Json.Formatting.Indented }) { JsonSerializer.CreateDefault().Serialize(writer, xml); } return sb.ToString(); }
Sample fiddle.