Solved

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

Posted on 2014-11-09
3
9 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
3 Comments
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
Comment Utility
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
Comment Utility
Thank you for your solution, will test it and getback to you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

6 Experts available now in Live!

Get 1:1 Help Now