Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6939621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:38:44+00:00 2026-05-27T12:38:44+00:00

I use the following code to validate an XML file against an XSD file.

  • 0

I use the following code to validate an XML file against an XSD file. It succesfully invokes the validation handler when errors are found and the value of xmlns in the XML file is valid. When it is invalid, the validation handler is not invoked.

private void ui_validate_Click(object sender, EventArgs e)
{
    try
    {
        ui_output.Text = "";

        XmlDocument xml_document = new XmlDocument();
        xml_document.LoadXml(ui_XML.Text);
        xml_document.Schemas.Add(null, XmlReader.Create(new System.IO.StringReader(ui_XSD.Text)));
        xml_document.Validate(validation_handler);
    }
    catch (Exception ex)
    {
        ui_output.Text = "Exception: " + ex.Message;
    }
}

private void validation_handler(object sender, ValidationEventArgs e)
{
    switch (e.Severity)
    {
        case XmlSeverityType.Error:
            ui_output.Text += "Error: " + e.Message + Environment.NewLine;
            break;
        case XmlSeverityType.Warning:
            ui_output.Text += "Warning: " + e.Message + Environment.NewLine;
            break;
    }
}

Update
An example for the accepted answer:

XmlDocument xml_document = new XmlDocument();
xml_document.Load(@"C:\temp\example.xml");
xml_document.Schemas.Add(null, @"C:\temp\example.xsd");
xml_document.Schemas.Compile();

XmlQualifiedName xml_qualified_name = new XmlQualifiedName(xml_document.DocumentElement.LocalName, xml_document.DocumentElement.NamespaceURI);
bool valid_root = xml_document.Schemas.GlobalElements.Contains(xml_qualified_name);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:38:45+00:00Added an answer on May 27, 2026 at 12:38 pm

    The way I handle this is to actually check first that for the Document Element (the root element) there is an XmlSchemaElement in your XmlReaderSettings.Schemas; if there isn’t, you can’t run the validation, which is why you get no error.

    So, make sure that your XmlSchemaSet is compiled; then build an XmlQualifiedName using the LocalName and NamespaceUri; use that to lookup an XmlSchemaElement, using GlobalElements.

    You should attempt to validate only if i) your schemas compile successfully and ii) you actually have a definition for the root element of the document.

    Hope it helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to compile a cpp file to object file. g++
I use the following code to validate users through windows authentication for my winform
When I validate the following code with jslint I get the following errors. function
I'm trying to use jquery validate. Following the example: http://docs.jquery.com/Plugins/Validation works great and I'd
I use the following code to create countdowns in Javascript. n is the number
I use the following code try to create an array of string vectors, I
I use the following code to layout network drives on a system. I want
I use the following code: Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3);
I use the following code to allocate a Console for a WinForm application. The
I use the following code: - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.