c# – Error converting from xmlschemaset to xmlnode
I am trying to read json file and parse using xsd and generate xml output from it. But my code is throwing build error. cannot convert from ‘System.Xml.Schema.XmlSchemaSet’ to ‘System.Xml.XmlNode’
static void Main(string[] args)
{
string json = File.ReadAllText(@"C:\Test\json1.json");
string xsd = File.ReadAllText(@"C:\Test\xsd1.xsd");
bool isValid = ValidateJsonAgainstXsd(json, xsd);
if (!isValid)
{
Console.WriteLine("JSON is not valid according to the XSD.");
return;
}
XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(json, "root");
xmlDoc.Save(@"C:\Test\my.xml");
}
static bool ValidateJsonAgainstXsd(string json, string xsd)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsd)));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(JsonConvert.DeserializeXmlNode(json).OuterXml);
bool isValid = true;
xmlDoc.Validate((sender, e) =>
{
Console.WriteLine(e.Message);
isValid = false;
}, schemas);
return isValid;
}
The error comes when its trying to use schemas variable
Read more here: Source link
