Adsony
asked on
Need to check that XML file has the right structure
I am writing a small application that generates unique codes for me. As I do not whish to have duplicates I want to make use of an xml file to store the generated codes for checking against any newly created codes.
I want to be able to assign the xml file that is used for the checking and as such have to validate that the file contains the correct xml.
I have created an XSD file and tried to validate it that way, but it does not seem to work. I intentionally use an incorrect xml file yet it validates.
This is the xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XSDSchema1" targetNamespace="http://tempuri.org/XSDSchema1.xsd" elementFormDefault="qualif ied" xmlns="http://tempuri.org/XSDSchema1.xsd" xmlns:mstns="http://tempuri.org/XSDSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="machineKeys">
<xs:sequence>
<xs:element name="machinekey" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="validationKey" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="^[A-Za-z0-9]{128}$" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="decryptionKey">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="^[A-Za-z0-9]{48}$" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Here is the code that is supposed to validate the xml file:
If ofd.ShowDialog = Windows.Forms.DialogResult .OK Then
Try
Dim rdr = New XmlTextReader(ofd.FileName )
Dim sr As StreamReader = New StreamReader(AppDomain.Cur rentDomain .BaseDirec tory & "MachineKeys.xsd")
Dim sch As XmlSchema = New XmlSchema
sch = XmlSchema.Read(sr, New ValidationEventHandler(Add ressOf ValidationEventHandler))
Dim rdrSettings As XmlReaderSettings = New XmlReaderSettings
rdrSettings.ValidationType = ValidationType.Schema
rdrSettings.Schemas.Add(sc h)
AddHandler rdrSettings.ValidationEven tHandler, New ValidationEventHandler(Add ressOf ValidationEventHandler)
Dim objXmlReader As XmlReader = XmlReader.Create(rdr, rdrSettings)
While objXmlReader.Read
End While
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Machine Key Generator")
End Try
End If
Private Sub ValidationEventHandler(ByV al sender As Object, ByVal e As Xml.Schema.ValidationEvent Args)
Select Case e.Severity
Case Xml.Schema.XmlSeverityType .Error
_xmlValidationOutcome &= "Error: " & e.Message & vbNewLine
Case Xml.Schema.XmlSeverityType .Warning
_xmlValidationOutcome &= "Warning {0}" & e.Message & vbNewLine
End Select
End Sub
PS: The XSD file is created if not found.
I want to be able to assign the xml file that is used for the checking and as such have to validate that the file contains the correct xml.
I have created an XSD file and tried to validate it that way, but it does not seem to work. I intentionally use an incorrect xml file yet it validates.
This is the xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XSDSchema1" targetNamespace="http://tempuri.org/XSDSchema1.xsd" elementFormDefault="qualif
<xs:complexType name="machineKeys">
<xs:sequence>
<xs:element name="machinekey" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="validationKey" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="^[A-Za-z0-9]{128}$"
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="decryptionKey">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="^[A-Za-z0-9]{48}$" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Here is the code that is supposed to validate the xml file:
If ofd.ShowDialog = Windows.Forms.DialogResult
Try
Dim rdr = New XmlTextReader(ofd.FileName
Dim sr As StreamReader = New StreamReader(AppDomain.Cur
Dim sch As XmlSchema = New XmlSchema
sch = XmlSchema.Read(sr, New ValidationEventHandler(Add
Dim rdrSettings As XmlReaderSettings = New XmlReaderSettings
rdrSettings.ValidationType
rdrSettings.Schemas.Add(sc
AddHandler rdrSettings.ValidationEven
Dim objXmlReader As XmlReader = XmlReader.Create(rdr, rdrSettings)
While objXmlReader.Read
End While
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Machine Key Generator")
End Try
End If
Private Sub ValidationEventHandler(ByV
Select Case e.Severity
Case Xml.Schema.XmlSeverityType
_xmlValidationOutcome &= "Error: " & e.Message & vbNewLine
Case Xml.Schema.XmlSeverityType
_xmlValidationOutcome &= "Warning {0}" & e.Message & vbNewLine
End Select
End Sub
PS: The XSD file is created if not found.
ASKER
Hi EverLearningCodeMonkey.
The XSD document is fine yes. What I want to do is take any xml document and check that it looks like what that xsd file sais it should.
For example: If i pass it a xml document that looks like -
<contactlist>
<contact>
<name>John Doe</name>
<number>555123456</number>
</contact>
</contactlist>
- I want the validator fail the document. Only xml documents that look like what that xsd doc says should pass.
The XSD document is fine yes. What I want to do is take any xml document and check that it looks like what that xsd file sais it should.
For example: If i pass it a xml document that looks like -
<contactlist>
<contact>
<name>John Doe</name>
<number>555123456</number>
</contact>
</contactlist>
- I want the validator fail the document. Only xml documents that look like what that xsd doc says should pass.
Ah, Gotcha.
I'll admit I'm not terribly familiar with XSD and document validation, I did however come across this article on MSDN, http://msdn2.microsoft.com/en-us/library/as3tta56.aspx - I'm not sure if it's equivalent to what you're using already but it may be worth taking a look at.
It implements XmlSchemaSet to act as a library that caches the schemas to improve performance. The code looks to be pretty similar to what you're using - I have noticed some minor differences in how some of the calls are made in the example vs how you have them, might be worth looking into as well.
I'll admit I'm not terribly familiar with XSD and document validation, I did however come across this article on MSDN, http://msdn2.microsoft.com/en-us/library/as3tta56.aspx - I'm not sure if it's equivalent to what you're using already but it may be worth taking a look at.
It implements XmlSchemaSet to act as a library that caches the schemas to improve performance. The code looks to be pretty similar to what you're using - I have noticed some minor differences in how some of the calls are made in the example vs how you have them, might be worth looking into as well.
ASKER
Read my original post. Then read my code.
First you told me something I already knew: my XSD schema is fine. Then you point me to some code that does exactly what my code is doing. This is not helping.
First you told me something I already knew: my XSD schema is fine. Then you point me to some code that does exactly what my code is doing. This is not helping.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now that's what I was looking for. Sounds like you are onto the answer there. I'll test it tomorow, my pc crashed so I'm doing search and rescue at the moment. I will let you know how it turns out. Thank you for the effort.
No Problem - Good luck with your search and rescue efforts
ELCM
ELCM
ASKER
I have not had a chance to check the above solution out yet. I have other projects that are keeping me busy. It sounds like it could just do the trick so I'm not going to string EverLearningCodeMonkey along until I can spare some time to actually try it. I therefore award the points to him. I'll post the outcome once I have a chance.
I havnen't taken too close a look at your code but I have looked at your XML and ran it through a couple of online validators (<a href="http://www.w3schools.com/dom/dom_validate.asp">http://www.w3schools.com/dom/dom_validate.asp</a> and <a href="http://www.w3.org/2001/03/webdata/xsv">http://www.w3.org/2001/03/webdata/xsv</a> respectively) and the XML (at least what you posted IS valid, at least according to them.
I guess what I'm wondering is what you mean in terms of validation? Structurally the XML is fine, whether the data that it contains is what your application wants/needs may be another matter entirely.
From what I gather from the code you posted, it's evaluating the structure of the XML document. To validate for application specific information I think you'll have to navigate your way through the nodes and check it that way.
Hope this helps.