Need to check that XML file has the right structure

Posted on 2006-11-29
Last Modified: 2013-11-19
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="" elementFormDefault="qualified" xmlns="" xmlns:mstns="" xmlns:xs="">
  <xs:complexType name="machineKeys">
      <xs:element name="machinekey" minOccurs="0">
            <xs:element name="validationKey" minOccurs="1" maxOccurs="1">
                <xs:restriction base="xs:string">
                  <xs:pattern value="^[A-Za-z0-9]{128}$" />
            <xs:element name="decryptionKey">
                <xs:restriction base="xs:string">
                  <xs:pattern value="^[A-Za-z0-9]{48}$" />

Here is the code that is supposed to validate the xml file:

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim rdr = New XmlTextReader(ofd.FileName)
        Dim sr As StreamReader = New StreamReader(AppDomain.CurrentDomain.BaseDirectory & "MachineKeys.xsd")
        Dim sch As XmlSchema = New XmlSchema

        sch = XmlSchema.Read(sr, New ValidationEventHandler(AddressOf ValidationEventHandler))
        Dim rdrSettings As XmlReaderSettings = New XmlReaderSettings
        rdrSettings.ValidationType = ValidationType.Schema

        AddHandler rdrSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf 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(ByVal sender As Object, ByVal e As Xml.Schema.ValidationEventArgs)
    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.
Question by:Adsony
  • 4
  • 4

Expert Comment

ID: 18036929
Hi Adsony,

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=""></a> and <a href=""></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.

Author Comment

ID: 18037207
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 -
    <name>John Doe</name>

 - I want the validator fail the document. Only xml documents that look like what that xsd doc says should pass.

Expert Comment

ID: 18038382
Ah, Gotcha.

I'll admit I'm not terribly familiar with XSD and document validation, I did however come across this article on MSDN, - 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.

Author Comment

ID: 18043420
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.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Accepted Solution

EverLearningCodeMonkey earned 500 total points
ID: 18045515
Hi Adsony,

I have read your original post and your code.  The first time around I admit I misunderstood.  My second post was to point you to some test bed code.  It's a working example that you can test, as I have this morning.  It works just fine with the data for the example.

My point in suggesting it was to provide a scenario to compare and contrast what you've written yourself.  If your code produces the same results as the example then all is well with what you've written and then you can focus other things that are possibly causing problems.

I ran both the MSDN sample and your code side by side and they both produce the same results.  So your code is fine too.   Given your previous post I have a mental image of you screaming "THAT DOESN'T HELP EITHER!" - I would contend that it does help as it removes your code as the potential problem.  The next logical step is to turn to the XML and XSD files themselves and start messing with copies of them.

I can recreate the scenario that you describe where you run an XML document through the validation and it should fail but it doesn't.  If the XML document I'm trying to validate doesn't have the same targetNamespace as the schema or is devoid of one then it generates no errors.  If the correct targetNamespace is specified then it will validate the file properly.

So, at least from where I'm standing, the focus would now be how to get the validator to recognize that the namespace is wrong or missing.  After a little playing around with the properties of the objects I've found that the XmlReader object has a property called NamespaceURI which is a string that has the namespace in it - apparently you need to start reading the document in order to get this property but it is there.  The XmlSchema object has a similar property called TargetNamespace (also returns a string), you could do a simple string comparison as a means of checking namespaces and throwing an exception if they don't match or if the NamespaceURI property is missing.

I think that should just about do it.

Author Comment

ID: 18045637
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.

Expert Comment

ID: 18045751
No Problem - Good luck with your search and rescue efforts


Author Comment

ID: 18099994
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.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Getting multiple values from a dataset ASP.NET(VB) 3 37
Need help on C# Linq query on object 6 53
Web Form VB.Net  import CSV 4 27
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now