in Education by
Here is my function. If you pass MemoryStream to XmlReader it does not validate proper xml files sometimes. I have the XmlDocument object stored in memory, I want to validate it against the xsd Schema files provided by the end user. ValidateSchema1(string XMLPath, string XSDPath) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(XMLPath); using (MemoryStream mstream = new MemoryStream()) { //StreamWriter writer = new StreamWriter(mstream); xmlDocument.Save(mstream); mstream.Seek(0, SeekOrigin.Begin); XmlSchemaSet sc = new XmlSchemaSet(); // Add the schema to the collection. sc.Add(null, XSDPath); // Set the validation settings. XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; settings.ValidationEventHandler += ValidationCallBack; // Create the XmlReader object. // Not woking XmlReader reader = XmlReader.Create(mstream, settings); // Working //XmlReader reader = XmlReader.Create(new StringReader(xmlDocument.InnerXml), settings); // Working //XmlReader reader = XmlReader.Create(XMLPath, settings); // Parse the file. while (reader.Read()) ; } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This might work: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23387252.html This works How to validate, on runtime, xml against xsd without save the xsd file on local folder? Edit 1: Fixed the code you provided, now the code works as it should, validated 2 of my files. The reason why you got the error is the fact that you where trying to validate and Xsd with itself and the root element wasn't there. Please check the solution to see for yourself. public void Xsd_whithout_saved() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"file.xsd"); //In the futute, strArquivoInteiro will be fullfill by xsd comming from database as nvarchar(max) and I will //not be allowed to save as a file locally string strArquivoInteiro = xmlDoc.OuterXml; byte[] byteArray = Encoding.ASCII.GetBytes(strArquivoInteiro); MemoryStream streamXSD = new MemoryStream(byteArray); //<<< streamXSD.Position = 0; StreamReader readerXsd = new StreamReader(streamXSD); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationEventHandler += this.MyValidationEventHandler; settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(null, XmlReader.Create(readerXsd)); settings.CheckCharacters = true; XmlReader XmlValidatingReader = XmlReader.Create(@"file.xml", settings); XmlTextReader Reader = new XmlTextReader(@"file.xsd"); //Created another reader for xml to use for validation XmlTextReader Reader2 = new XmlTextReader(@"file.xml"); XmlSchema Schema = new XmlSchema(); //IN THIS LINE I RECEIVED THE XmlException "Root Element is Missing" and I can't understand the reason //This was the first problem, a xsd root element isn't equal to an xml root element , and you where trying to validate and xsd with xsd here, and of course the error. Schema = XmlSchema.Read(Reader, MyValidationEventHandler); XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader2); ValidatingReader.ValidationType = ValidationType.Schema; ValidatingReader.Schemas.Add(Schema); try { XmlValidatingReader.Read(); XmlValidatingReader.Close(); ValidatingReader.ValidationEventHandler += MyValidationEventHandler; while ((ValidatingReader.Read())) { } ValidatingReader.Close(); } catch (Exception ex) { ValidatingReader.Close(); XmlValidatingReader.Close(); } }

Related questions

0 votes
    I created a REST api and want to validate the body and the params before calling the controller logic. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    As stated in the title, i'm looking for an XML schema (XSD-file) for the Glade markup language? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I'm trying to insert data to the database and at the same time uploading an image to the path. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    If I want to use the validation framework that you can use with ASP.NET MVC, will the JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a strange problem. I'm executing insert using prepared statement like this: try (Connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have installed Docker Desktop for windows Docker version 18.09.2, build 6247962, and I'm not able ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to implement continuous integration and continuous deployment to my DEV Azure App Service. I'm ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm getting into user scripting with tampermonkey and can't get through this error, any help would be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am using module jsonwebtoken 8.4.0 in a nodejs 10.2.0 app. A JWT token is generated on https: ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    My goal is to host Django app on CentOs 7 with python3.10 I've manage to download and configure ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I've been experimenting with cronjobs recently and have it setup like so: crontab: SHELL=/bin/sh MAILTO= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    What's the best practice (or tool) for updating/migrating Mongoose schemas as the application evolves? ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    What's the best practice (or tool) for updating/migrating Mongoose schemas as the application evolves? ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
...