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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:12:36+00:00 2026-05-15T03:12:36+00:00

As a follow up to my issue transforming large Xml files , I now

  • 0

As a follow up to my issue transforming large Xml files, I now need to validate the schema.

I was using this extension method, which can clearly be improved upon as it’s not working correctly either

public static XElement ValidateXsd(this XElement source, string xsdPath)
{
    var errors = new XElement("Errors");

    // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx
    var xsd = XDocument.Load(xsdPath);
    var xml = XDocument.Load(source.CreateReader());

    var schemas = new XmlSchemaSet();
    schemas.Add("", xsd.CreateReader());

    if (xml.Document != null)
    {
        xml.Document.Validate(schemas,
            // Validation Event/Error Handling
            (sender, e) =>
                {
                    var message = e.Message
                        .Replace(
                            "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
                            "cannot be blank.")
                        .Replace(
                            "is invalid according to its datatype 'size' - The Pattern constraint failed.",
                            "must be numeric.")
                        .Replace(
                            "element is invalid",
                            "is invalid.");

                    errors.Add(new XElement("Error", message));
                }
            );
    }

    // If there were errors return them, otherwise return null
    return errors.Elements().Count() > 0 ? errors : null;
}
  • 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-15T03:12:37+00:00Added an answer on May 15, 2026 at 3:12 am

    Try this instead:

    public static XElement ValidateXsd(this XElement source, string xsdPath)
    {
        var errors = new XElement("Errors");
    
        // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx 
    
        var schemas = new XmlSchemaSet();
        using (var reader = XmlReader.Create(xsdPath))
        {
            schemas.Add("", reader);
        }
    
        {
            source.Document.Validate(
                schemas,
                // Validation Event/Error Handling 
                (sender, e) =>
                {
                    var message =
                        e.Message.Replace(
                            "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
                            "cannot be blank.").Replace(
                            "is invalid according to its datatype 'size' - The Pattern constraint failed.",
                            "must be numeric.").Replace("element is invalid", "is invalid.");
    
                    errors.Add(new XElement("Error", message));
                });
        }
    
        // If there were errors return them, otherwise return null 
        return errors.Elements().Count() > 0 ? errors : null;
    } 
    

    Try this instead:

    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Schema;
    
        static class Extensions
        {
            public static XElement ValidateXsd(this XElement source, string xsdPath)
            {
                var errors = new XElement("Errors");
    
                // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx 
    
                var schemas = new XmlSchemaSet();
                using (var reader = XmlReader.Create(xsdPath))
                {
                    schemas.Add("", reader);
                }
    
                var sourceQualifiedName = new XmlQualifiedName(source.Name.LocalName, source.Name.NamespaceName);
                source.Validate(
                    schemas.GlobalElements[sourceQualifiedName],
                    schemas,
                    // Validation Event/Error Handling 
                    (sender, e) =>
                    {
                        var message =
                            e.Message.Replace(
                                "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
                                "cannot be blank.").Replace(
                                "is invalid according to its datatype 'size' - The Pattern constraint failed.",
                                "must be numeric.").Replace("element is invalid", "is invalid.");
    
                        errors.Add(new XElement("Error", message));
                    });
    
                // If there were errors return them, otherwise return null 
                return errors.Elements().Count() > 0 ? errors : null;
            } 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up issue relating to the answer for SQL placeholder in
This is a follow up to a previous issue I had posted here .
I'm having issue converting this string to a decimal. I tried to follow the
i have the follow issue : I have a point which is setted at
Just to follow up on here: Creating very large image files with BufferedImage, strange
This is a follow up question to Using 301/303/307 redirects for dynamic short urls
I have an issue I have this jQuery code: $(document).ready(function(){ $(#follow).click(function(){ $.ajax({ type: 'POST',
This is a related (and or follow up) issue to : Event Function called
I'm facing a nasty encoding issue when transforming XML via XSLT through PHP. The
This is a follow up question from here: linq issue with creating relationships in

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.