Possible to validate xml against xsd using code at runtime?

后端 未结 3 1917
一生所求
一生所求 2020-12-14 04:21

I have xml files that I read in at runtime, is is possible to validate the xml against an xsd file at runtime? using c#

3条回答
  •  长情又很酷
    2020-12-14 04:53

    simpler solution..

            try
            {
                XmlReaderSettings Xsettings = new XmlReaderSettings();
                Xsettings.Schemas.Add(null, "personDivideSchema.xsd");
                Xsettings.ValidationType = ValidationType.Schema;
    
                XmlDocument document = new XmlDocument();
                document.Load("person.xml");
    
                XmlReader reader = XmlReader.Create(new StringReader(document.InnerXml), Xsettings);
    
    
                while (reader.Read());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
    

提交回复
热议问题