Jump to content

validating the created XML against the given XSD


svibuk

Recommended Posts

i have a xml file & a given xsdXML file will be created from any source of dataafter the file creation i need to just validate/ check the data against the given xsd ie if a field is defined a numeric and if any charcter is entered in it than it shld display the invalid data error msgif a field needs to contain a date field & if the data is invalid as checked against the xsd it shld display the message accordinglyis this way of validation for XML possible against a given XSD??or how do i solve this issue

Link to comment
Share on other sites

Not only is it possible, but it's exactly how XSD was intended to be used.The only question is what language are you using for the XML generation or what language should receive the XML? There are many ways to initiate an XSD validation, but they all depend on the language, and the supported libraries you have for it.

Link to comment
Share on other sites

Not only is it possible, but it's exactly how XSD was intended to be used.The only question is what language are you using for the XML generation or what language should receive the XML? There are many ways to initiate an XSD validation, but they all depend on the language, and the supported libraries you have for it.
Thnaks for u r postoive reply as since many days i am trying for it but no idea where to start upi am using vb.net for creating XML file from SQL databaselet me knw in vb.net how do i do it i am give a XML file which conatins data & an xsd fileon a button click i need to do the validation
Link to comment
Share on other sites

XML utilities have evolved a lot between .NET versions. I'm going to show you the version 4 way, but if your host has an earlier .NET version, you might want to read over the linked manual pages for any changes...1. You create an instance of the XmlDocument class.2. Load() the document... as a stream if the XML was generated on the fly, or as a string pointing to a generated file.3. You create a separate function with the following prototype (taken from the example in the link below; "myMethod" can be anything you want - it's just the name of the new function):

Shared Sub myMethod(ByVal sender As Object, ByVal e As ValidationEventArgs)

4. Call the Validate() method with a ValidationEventHandler instance that has the name of the new function as an argument. See example in the link if that sentence doesn't make any sence.Now... what your separate function will receive is a notification of any errors, including error description. If the function doesn't execute even once, on the next line after the Validate() method, you can safely assume there are no errors or warnings. It's up to your separate function to decide what to do for every encountered error. What I'd do is also create a separate variable indicating whether the document is valid or not, and set it to true by default. For every execution of the validation handler (the separate function), set it to false, and handle message display further below there. After the Validate() call, check the separate variable, and proceed if it's true.

Link to comment
Share on other sites

XML utilities have evolved a lot between .NET versions. I'm going to show you the version 4 way, but if your host has an earlier .NET version, you might want to read over the linked manual pages for any changes...1. You create an instance of the XmlDocument class.2. Load() the document... as a stream if the XML was generated on the fly, or as a string pointing to a generated file.3. You create a separate function with the following prototype (taken from the example in the link below; "myMethod" can be anything you want - it's just the name of the new function):
Shared Sub myMethod(ByVal sender As Object, ByVal e As ValidationEventArgs)

4. Call the Validate() method with a ValidationEventHandler instance that has the name of the new function as an argument. See example in the link if that sentence doesn't make any sence.Now... what your separate function will receive is a notification of any errors, including error description. If the function doesn't execute even once, on the next line after the Validate() method, you can safely assume there are no errors or warnings. It's up to your separate function to decide what to do for every encountered error. What I'd do is also create a separate variable indicating whether the document is valid or not, and set it to true by default. For every execution of the validation handler (the separate function), set it to false, and handle message display further below there. After the Validate() call, check the separate variable, and proceed if it's true.

Hii am new to XML & XSD so if u culd explian me from the start as to how to go abt it with the existing test.xml & test.xsd file that i havei have just got a aspx page with a command buttonu r step by step guidance will make it easy for me to undertsnad & startas currently i dont need to create any xml or xsd i just need to do the validation on button click with the given filescurrently i am referring http://vbcity.com/forums/p/129777/553868.aspx which has a good examplebut the only prblm is i not able to select any other xml documenti need to allow the users to select the xml docuument & the associated xsd shd automically be taken is there any such example
Link to comment
Share on other sites

Do you know how to attach a method to the button's click event?Do all of the steps above on that event. Try it with your test files first, and only then try to make it work for any file.Don't know how to attach a function to a button's click event?1. Open Visual Studio and create a new "Windows Forms" project.2. From the toolbox (should be on the left, but that's not for certain; click "View > Toolbox" to open it if it's not, and/or get it highlighted), drag&drop the "Button" control somewhere onto the Window in the middle.3. Double click the new button. Visual Studio should redirect you right into a newly generated method that will be executed when the button is clicked.I once saw a video tutorial by Microsoft that showed that, but I can't find it anymore...Also, I forgot... somewhere between steps 1 and 4 on the previous post, you also need to do:

xml.Schemas.Add(null, "test.xsd");

Where "xml" is the XmlDocument instance (created in step 1), "test.xsd" is obviously the XSD file to validate against, and the null is the namespace the XSD is for. I'm assuming your XML document has no namespaces, so use null here.

Link to comment
Share on other sites

Do you know how to attach a method to the button's click event?Do all of the steps above on that event. Try it with your test files first, and only then try to make it work for any file.Don't know how to attach a function to a button's click event?1. Open Visual Studio and create a new "Windows Forms" project.2. From the toolbox (should be on the left, but that's not for certain; click "View > Toolbox" to open it if it's not, and/or get it highlighted), drag&drop the "Button" control somewhere onto the Window in the middle.3. Double click the new button. Visual Studio should redirect you right into a newly generated method that will be executed when the button is clicked.I once saw a video tutorial by Microsoft that showed that, but I can't find it anymore...Also, I forgot... somewhere between steps 1 and 4 on the previous post, you also need to do:
xml.Schemas.Add(null, "test.xsd");

Where "xml" is the XmlDocument instance (created in step 1), "test.xsd" is obviously the XSD file to validate against, and the null is the namespace the XSD is for. I'm assuming your XML document has no namespaces, so use null here.

i am referring http://msdn.microsoft.com/en-us/library/zc...5(v=VS.80).aspxas per the thread u have postedsome basix queriesi have taken a aspx page and a command button (VALIDATE)i tried to use the code frm above linkhow do i make the code workable on the aspx page on the button clickaatched is the code==============================Imports SystemImports System.XmlImports System.Xml.SchemaImports System.IOImports Microsoft'Microsoft.Samples.XmlPartial Public Class _Default Inherits System.Web.UI.Page Protected Sub cmdvalidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdvalidate.Click End SubEnd ClassNotInheritable Class XmlDocumentSample Dim instance As New XmlDocument Private Sub New() End Sub Shared reader As XmlReader Shared filename As String = "bookdtd.xml" Public Shared Sub Main() Dim eventHandler As New ValidationEventHandler(AddressOf XmlDocumentSample.ValidationCallback) Try ' Create the validating reader and specify DTD validation. Dim settings As New XmlReaderSettings() settings.ProhibitDtd = False settings.ValidationType = ValidationType.DTD AddHandler settings.ValidationEventHandler, eventHandler reader = XmlReader.Create(filename, settings) ' Pass the validating reader to the XML document. ' Validation fails due to an undefined attribute, but the ' data is still loaded into the document. Dim doc As New XmlDocument() doc.Load(reader) Console.WriteLine(doc.OuterXml) Finally If Not (reader Is Nothing) Then reader.Close() End If End Try End Sub ' Display the validation error. Private Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs) MsgBox("Validation error loading: {0}", filename) MsgBox(args.Message) End SubEnd Class
Link to comment
Share on other sites

Do you know how to attach a method to the button's click event?Do all of the steps above on that event. Try it with your test files first, and only then try to make it work for any file.Don't know how to attach a function to a button's click event?1. Open Visual Studio and create a new "Windows Forms" project.2. From the toolbox (should be on the left, but that's not for certain; click "View > Toolbox" to open it if it's not, and/or get it highlighted), drag&drop the "Button" control somewhere onto the Window in the middle.3. Double click the new button. Visual Studio should redirect you right into a newly generated method that will be executed when the button is clicked.I once saw a video tutorial by Microsoft that showed that, but I can't find it anymore...Also, I forgot... somewhere between steps 1 and 4 on the previous post, you also need to do:
xml.Schemas.Add(null, "test.xsd");

Where "xml" is the XmlDocument instance (created in step 1), "test.xsd" is obviously the XSD file to validate against, and the null is the namespace the XSD is for. I'm assuming your XML document has no namespaces, so use null here.

http://www.java2s.com/Tutorial/ASP.NET/050...XMLSchemaVB.htmwith reference to above link i am able to sucessfully validate the given xml against the xsd in dot netis it the right way??how do i do the same process using VB 6.0
Link to comment
Share on other sites

Yes and no... it's the right way if you want to validate as you go (every node is validated as you reach it, and error messages are emitted as you reach to an erronous point).If you want to validate the whole document at once, you need to use the approach I outlined for you above.I'm not sure how I can say it in a simpler fashion, other than copy&paste code (which I can't currently give...).Except now I realize you're using ASP.NET, and not just a desktop .NET app... so... it's still pretty much the same deal, except you need to create an ASP.NET page.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...