0
  • We create an xsd schema that matches a json
  • generate a c# class from the xsd schema
  • Use newtonsoft.json to deserialize the json to object and object to xml(based on the generated class)

This is needed as the product we use can only deal with xml and not json. This works fine for most of the json responses.

Now we have to support additional properties(any) in the incoming json. So we have created a xsd schema with an "Any" element with "ProcessContents = lax". The generated class contains the any element as shown below.

public System.Xml.XmlElement Any
   {
    get { return this.anyField; }
    set { this.anyField = value; }
   }

The following is the behaviour with different Json inputs.

  • The json must contain the name "Any" to match the any element. This does not fulfil the additional properties idea.
  • I could add a "JsonProperty("SomeName")" but still I should know the additional properties name which I wouldnt.

Could someone provide a way to do this please. Thanks.

Edit1: After suggestions to use JsonExtensionData, the class looks like this. enter image description here

I see that the additionalproperties are deserialized to an object as seen in the image below. enter image description here Error received : "Cannot serialize member .AdditionalProperties of type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary."} Question: How tio solve this and serialize this to XmlAny Element? Thanks.

SriramN
  • 432
  • 5
  • 19
  • 1
    Check this thread: https://stackoverflow.com/questions/15253875/deserialize-json-with-known-and-unknown-fields – Quercus Nov 16 '20 at 18:17
  • @HereticMonkey,Quercus could you pls help here? Have saved the result as an edit, able to convert json to object but object to xml is having an error.Kindly help. Thanks. – SriramN Nov 17 '20 at 12:26
  • I suggest finding some tutorials on searching. I went to Google and searched for `site:stackoverflow.com c# serialize dictionary to xml` and found "about 9,880 results", one of which is [How to XML-serialize a dictionary](https://stackoverflow.com/q/3671259/215552). This is programming; take each problem, search for an answer, continue as needed. – Heretic Monkey Nov 17 '20 at 12:51
  • :| I was on the same link and trying it out. But had an issue that's why posted. But I get your point, a little more persistence :)Thanks for the directions. – SriramN Nov 18 '20 at 06:19

1 Answers1

0

Rather than parsing JSON to an Object and then the Object to XML why not just do JSON -> XML?

Newtonsoft provides a method for doing exactly that.

var node = JsonConvert.DeserializeXNode(jsonString, "Root");

https://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm

Edit: After reading back I realise it's a business requirement to use the XSD Schema.

DMck
  • 50
  • 4
  • We would like to have more control and be sure on how the Json to Xml conversion happens. So an object provides a way to ensure this.Also yes, is a business requirement.Thanks. – SriramN Nov 17 '20 at 05:08