I am creating a C# Application using Web Services. In my Web Services I\'m using a JSONString
data.
But I\'m not able to convert this string into a DataSe
As a dynamic C# solution(when you don't know the object structure to deserialize) by using @Dhaval's answer and after invaliding Deserialize<>()
method I use below method to do that:
Update: DataSet.ReadXml
has some options in reading XML node as XmlReadMode:
private static DataSet ReadDataFromJson(string jsonString, XmlReadMode mode = XmlReadMode.Auto)
{
//// Note:Json convertor needs a json with one node as root
jsonString = $"{{ \"rootNode\": {{{jsonString.Trim().TrimStart('{').TrimEnd('}')}}} }}";
//// Now it is secure that we have always a Json with one node as root
var xd = JsonConvert.DeserializeXmlNode(jsonString);
//// DataSet is able to read from XML and return a proper DataSet
var result = new DataSet();
result.ReadXml(new XmlNodeReader(xd), mode);
return result;
}
E.g. If you want to infer a strongly typed schema from the data:
var dataset = ReadDataFromJson(yourString, XmlReadMode.InferTypedSchema);