I am converting an XML document into JSON.
I have a node that can have multiple nodes.
In order to implement the sequence of nodes in an array, I should add the json: array = true
attribute.
On my root node, I add json
namespace:
author. Writing attribute string ("xmlns", "json", tap, "http://james.newtonking.com/json");
Then I need to have an array on element I json: array = true
attribute:
Add author WriteAttributeString ( "Array", "http://james.newtonking.com/json", "true");
Appears as expected by XML:
& lt; Results xmlns: json = "http://james.newtonking.com/json" & gt; & Lt; Object json: Array = "true" & gt;
But JSON looks like this:
"Result": {"@xmlns: json": "http://james.newtonking.com / Json "," Object ": {" @Jason: Array ":" True ",
What am I doing?
You must have XML namespace incorrect: this should be:
http : //james.newtonking.com/projects/json
not
http: //james.newtonking.com/json
< / Pre>Working demo with correct namespace:
class program {static void main (string [] args) {stringbiller sb = new stringbilder (); StringWrite SW = New String Vter (SB); XmlTextWriter author = new XmlTextWriter (sw); String xmlns = "http://james.newtonking.com/projects/json"; Author.formatings = system XML formatting. Indented; Author.WriteStartDocument (); Author.WriteStartElement ("Result"); Author. White Unbreakable String ("XML," "Jason", Faucet, XML); Writer.WriteStartElement ("object"); Author. WhiteAttivit string ("array", XMLNS, "true"); Author.WriteStartElement ("foo"); Writer.WriteString ("bar"); Writer.WriteEndElement (); Writer.WriteEndElement (); Writer.WriteEndElement (); Writer.WriteEndDocument (); String xml = sb.ToString (); Console.WriteLine (XML); Console.WriteLine (); XmlDocument Document = New XmlDocument (); Doc.LoadXml (xml); String json = JsonConvert.SerializeXmlNode (doctor, Newtonsoft.Json.Formatting.Indented); Console.WriteLine (json); }}
Output:
& lt ;? XML version = "1.0" encoding = "UTF-16"? & Gt; & Lt; Results xmlns: json = "http://james.newtonking.com/projects/json" & gt; & Lt; Object json: Array = "true" & gt; & Lt; Foo & gt; Bar & lt; / Foo & gt; & Lt; / Object & gt; & Lt; / Results & gt; {"Xml": {"@version": "1.0", "@encoding": "utf-16"}, "result": {"object": [{"foo": "bar"}]}} < / Code>
Comments
Post a Comment