Reputation: 502
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.
Could someone provide a way to do this please. Thanks.
Edit1:
After suggestions to use JsonExtensionData, the class looks like this.
I see that the additionalproperties are deserialized to an object as seen in the image below.
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.
Upvotes: 0
Views: 932
Reputation: 50
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.
Upvotes: 0