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

The Archive Base Latest Questions

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

EDIT2: the problem seems to be my xsd. it validates pretty much every XML.

  • 0

EDIT2: the problem seems to be my xsd. it validates pretty much every XML. i can’t post the XSD on here though. why would every XML be valid for an XSD?

EDIT: found a similar excample in the answer on here. Same issue, it finds no errors no matter what xml and xsd i compare. even if i use a random diffrent xsd it keeps saying it is all fine.

I found a lot of examples doing it without LINQ, but how would you do it with LINQ?

I used Google to find an example but it seems to skip the validation most of the time validating every XML. (it once went into it, rejecting the file but I haven’t been able to reproduce it.)

Are there better ways of doing it or is there a reason why it would skip over the validation?

public String ValidateXml2(String xml, String xsd)
    {
        String Message = String.Empty;

        var ms = new MemoryStream(Encoding.Default.GetBytes(xml));

        // Create the XML document to validate against.
        XDocument xDoc = XDocument.Load(ms, LoadOptions.PreserveWhitespace);
        XmlSchemaSet schema = new XmlSchemaSet();

        bool isError = new bool();  // Defaults to false.
        int countError = 1;         // Counts the number of errors have generated.
        Stream xsdMemoryStream = new MemoryStream(Encoding.Default.GetBytes(xsd));

        // Add the schema file you want to validate against.
        schema.Add(XmlSchema.Read
                (xsdMemoryStream,
                    new ValidationEventHandler((sender, args) =>
                    {
                        Message = args.Exception.Message;
                    })
                ));

        // Call validate and use a LAMBDA Expression as extended method!
        // Don't you love .NET 3.5 and LINQ...
        xDoc.Validate(schema, (sender, e) =>
        {
            switch (e.Severity)
            {
                case XmlSeverityType.Error:
                    Console.WriteLine("Error {0} generated.", countError);
                    break;
                case XmlSeverityType.Warning:
                    Console.WriteLine("Warning {0} generated.", countError);
                    break;
            }
            Console.WriteLine(sender.GetType().Name);
            Console.WriteLine("\r\n{0}\r\nType {1}\r\n", e.Message,
                                                         e.Severity.ToString());

            Console.WriteLine("-".PadRight(110, '-'));
            countError++;
            isError = true; // If error fires, flag it to handle once call is complete.
        }
          , true); // True tells the validate call to populate the post-schema-validation
        // which you will need later, if you want to dive a littel deeper...

        if (isError == true) // Error has been flagged.  Lets see the errors generated.
            Console.WriteLine("You my friend have {0} error(s), now what?", countError);
        else
            Console.WriteLine("You rock! No errors...");

        Console.Write("\r\n\r\nPress Enter to End");
        Console.ReadKey();


        return Message;
    }

Credits and original example

  • 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-27T17:57:39+00:00Added an answer on May 27, 2026 at 5:57 pm

    Apparently using LINQ to XML you have to have the schemas targetNamespace match the namespace of the xml you are checking, because the validate method looks through the collection of schemas for one validating that documents namespace. If one is not found then the document is validated.

    check out this link for more info

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

Sidebar

Related Questions

Edit2: Can I do polymorphism with Union? It seems to me that I can
EDIT2 Looks like I can't read properly, disregard this post, I will try to
edit: I was trying to solve a spoj problem. Here is the link to
EDIT3: It seems that the problem occurs on my localhost XAMPP PHP 5.3 setup,
EDIT3: It seems that the problem occurs on my localhost XAMPP PHP 5.3 setup,
EDIT : Thanks to Howard, I've corrected the code here and it seems to
[Edit: This problem applies only to 32-bit systems. If your computer, your OS and
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
Edit 1: Uninstalled & Reinstalled Edit 2: Same problem. Seriously? Yes. I am having
Most of you probably know the following problem: You edit an HTML, view the

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.