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 7774239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:19:40+00:00 2026-06-01T17:19:40+00:00

I need to implement a C# method that needs to validate an XML against

  • 0

I need to implement a C# method that needs to validate an XML against an external XSD and return a Boolean result indicating whether it was well formed or not.

public static bool IsValidXml(string xmlFilePath, string xsdFilePath);

I know how to validate using a callback. I would like to know if it can be done in a single method, without using a callback. I need this purely for cosmetic purposes: I need to validate up to a few dozen types of XML documents so I would like to make is something as simple as below.

if(!XmlManager.IsValidXml(
    @"ProjectTypes\ProjectType17.xml",
    @"Schemas\Project.xsd"))
{
     throw new XmlFormatException(
         string.Format(
             "Xml '{0}' is invalid.", 
             xmlFilePath));
}
  • 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-06-01T17:19:42+00:00Added an answer on June 1, 2026 at 5:19 pm

    There are a couple of options I can think of depending on whether or not you want to use exceptions for non-exceptional events.

    If you pass a null as the validation callback delegate, most of the built-in validation methods will throw an exception if the XML is badly formed, so you can simply catch the exception and return true/false depending on the situation.

    public static bool IsValidXml(string xmlFilePath, string xsdFilePath, XNamespace namespaceName)
    {
        var xdoc = XDocument.Load(xmlFilePath);
        var schemas = new XmlSchemaSet();
        schemas.Add(namespaceName, xsdFilePath);
    
        try
        {
            xdoc.Validate(schemas, null);
        }
        catch (XmlSchemaValidationException)
        {
            return false;
        }
    
        return true;
    }
    

    The other option that comes to mind pushes the limits of your without using a callback criterion. Instead of passing a pre-defined callback method, you could instead pass an anonymous method and use it to set a true/false return value.

    public static bool IsValidXml(string xmlFilePath, string xsdFilePath, XNamespace namespaceName)
    {
        var xdoc = XDocument.Load(xmlFilePath);
        var schemas = new XmlSchemaSet();
        schemas.Add(namespaceName, xsdFilePath);
    
        Boolean result = true;
        xdoc.Validate(schemas, (sender, e) =>
             {
                 result = false;
             });
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement the method: object GetFactory(Type type); This method needs to return
I need to implement a method that takes an address split up into individual
I need to implement a call to a method that I have in my
I need to implement a method for non-linear interpolation between values, ease-in, ease-out, general
I need to implement in my class Invoke() method with the same behavior as
This is a question about how to implement the equals method when I need
I need to implement a custom handler for MVC that gives me the first
I'm trying to implement a payment processor. In order to validate that the response
Ok. 1) I need to validate :link in my model and do that only
I need to validate a form therefore I am writing a php class that

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.