Solved

Help with  validating specific xml file in a multiple folders using VB.NET

Posted on 2014-11-09
3
23 Views
Last Modified: 2016-06-23
Hi,

How do you modify the code below to validate xml files in multiple folders? For example I need to validate NSNBEL.XML in \Data\MasterNSN folder,  NSCBEL.xml in \Data\MasterNSN folder, but the .xsd files in the MasterFiles folder does not include the country, for example it contains NSN.xsd, NSC.xsd. Is there a way to but the folders in a string variable and use a loop instead of writing the same code for each folder and country. When I only one to validate for each country at a time not all countries in the folder.

 On Error GoTo tt
        Dim zz As String = ""
        For Each File As String In IO.Directory.GetFiles(Application.StartupPath + "\Data\MasterFuze\")
            Dim settings As New XmlReaderSettings
            settings.ValidationType = ValidationType.Schema

            settings.Schemas.Add(XmlSchema.Read(New XmlTextReader(Application.StartupPath + "\Data\MasterFiles\" & IO.Path.GetFileNameWithoutExtension(File) & ".xsd"), AddressOf ValidationHandler))

            Dim reader As XmlReader = XmlReader.Create(File, settings)
            zz = File

            Dim doc As New XmlDocument()
            doc.Load(reader)
        Next

        MsgBox("Master Files Validated")
        Exit Sub

Open in new window


Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 40437709
Should the folder name in lines 3 and 7 be the same?

You could make this code into a function to which you pass the folder you want to validate:
    Private Function ValidateXML(ByVal CheckFolder As String) As Boolean
        On Error GoTo tt
        Dim zz As String = ""
        Dim File As String
        For Each File In IO.Directory.GetFiles(Application.StartupPath & CheckFolder)
            Dim settings As New XmlReaderSettings
            settings.ValidationType = ValidationType.Schema

            settings.Schemas.Add(XmlSchema.Read(New XmlTextReader(Application.StartupPath & CheckFolder & IO.Path.GetFileNameWithoutExtension(File) & ".xsd"), AddressOf ValidationHandler))

            Dim reader As XmlReader = XmlReader.Create(File, settings)
            zz = File

            Dim doc As New XmlDocument()
            doc.Load(reader)
        Next

        MsgBox("Master Files Validated")
        Return True

tt:
        MsgBox("Validation Error in " & File)
        Return False

    End Function

Open in new window


Then call it with each folder you want to check:
        blnValid = ValidateXML("\Data\MasterFiles\")
        blnValid = ValidateXML("\Data\MasterNSN\")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40446951
Thank you for your solution, will test it and getback to you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

696 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