How can I serialize namespaces from an XElement using json.net

Ioninesleesse

New Member
I'm experimenting with Json.NET, trying to find an automatic way to translate the Xml documents generated by our business layer into JSON for the javascript on our web client.\[code\]JsonConvert.SerializeXObject()\[/code\] will serialize attributes, but not namespaces (even through they're really just another attribute)We generate wrappers for XElements using Linq to Xsd, which generates schema conforming Xml.So the test code goes like this:\[code\] input.Type = "Bob the type"; input.InputPayload = new InputPayload(); string test = JsonConvert.SerializeXNode(input.Untyped);\[/code\]Which generates the following XML:\[code\]<Input xmlns="http://tempuri.org/Test.xsd"> <Type>Bob the type</Type> <InputPayload xmlns="http://www.test.com/BL.xsd" /></Input>\[/code\]But the following JSON:\[code\]{"Input":{"Type":"Bob the type","InputPayload":null}}\[/code\]However, if I try to load that back into our wrappers, we get nothing in the wrapper's properties because the namespaces are missing.BUT, if I do THIS:\[code\] XDocument output = new XDocument( new XElement("ComplexType", new XAttribute("xmlns", "http://www.attributetest.com"), new XElement("CurrentDateTimeLocal", "test"), new XElement("CurrentDateTimeUTC", "Test"), new XElement("LocalTimeZone", "Test") ) );\[/code\]I get this Xml:\[code\]<ComplexType testattr="http://www.attributetest.com"> <CurrentDateTimeLocal>test</CurrentDateTimeLocal> <CurrentDateTimeUTC>Test</CurrentDateTimeUTC> <LocalTimeZone>Test</LocalTimeZone></ComplexType>\[/code\]And this JSON:\[code\]{"ComplexType":{"@testattr":"http://www.attributetest.com","CurrentDateTimeLocal":"test","CurrentDateTimeUTC":"Test","LocalTimeZone":"Test"}}\[/code\]Which uses an @ notation to denote attributes.This correctly de-serializes the XDocument, so it can handle attributes, just doesn't recognize namespaces.Does anybody know how to make this work with the existing Json.NET library or will I have to write support for this myself?Thanks,J.
 
Back
Top