Home All Groups Group Topic Archive Search About

IsXmlValid function

Author
5 Dec 2006 11:34 AM
Craig HB
I would like to validate an XML document in a VB.NET app.

This is the function that I'm after:

Public Shared Function IsXmlValid(ByVal xmlPath As String, ByVal xsdPath As
String) as Boolean
'code here
End Sub

Can someone help me with the code ?

Thanks,
Craig

Author
7 Dec 2006 11:48 PM
Peter Ritchie [C# MVP]
You'll need more than just XML file path and XSD file path, you'll also need
the namespace name as one XML file can use multiple schemas.  Here's an
example from MSDN of validating an XML file to a single schema:

void DoSomething()
{
    // Create the XmlSchemaSet class.
    XmlSchemaSet sc = new XmlSchemaSet();

    // Add the schema to the collection.
    sc.Add("urn:bookstore-schema", "books.xsd");

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = sc;
    settings.ValidationEventHandler += new ValidationEventHandler
(ValidationCallBack);

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

    // Parse the file.
    while (reader.Read());

  }

  // Display any validation errors.
  private static void ValidationCallBack(object sender, ValidationEventArgs
e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
  }

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Show quote
"Craig HB" wrote:

> I would like to validate an XML document in a VB.NET app.
>
> This is the function that I'm after:
>
> Public Shared Function IsXmlValid(ByVal xmlPath As String, ByVal xsdPath As
> String) as Boolean
> 'code here
> End Sub
>
> Can someone help me with the code ?
>
> Thanks,
> Craig

AddThis Social Bookmark Button