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
Solved

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

Posted on 2014-11-09
3
15 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
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 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