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

  • Home
  • SEARCH
  • 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 8403091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:15:10+00:00 2026-06-09T22:15:10+00:00

Having XML schema with the namespace http://mynamespace . If the wrong XML document with

  • 0

Having XML schema with the namespace http://mynamespace. If the wrong XML document with default namespace xmlns="http://mynamespace" is validated then exception is thrown as expected. While if someone change the namespace(http://Wrongnamespace) this wrong XML will pass validation.

Here is the unit test with schema validation. Method XSD_NotValid_2 is not working properly:

[TestClass]
public class XSDTest
{
    public System.Xml.XmlReaderSettings ReaderSettings
    {
        get
        {
            string sXSD = "<xsd:schema targetNamespace=\"http://mynamespace\" xmlns=\"http://mynamespace\""
                + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">"
            + "<xsd:element name=\"Root\">"
            + "<xsd:complexType>"
                + "<xsd:sequence>"
                    + "<xsd:element name=\"Child\" minOccurs=\"1\" maxOccurs=\"1\" />"
                + "</xsd:sequence>"
             + "</xsd:complexType>"
            + "</xsd:element>"
            + "</xsd:schema>";

            System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(new System.IO.StringReader(sXSD)
                        , new System.Xml.Schema.ValidationEventHandler(OnValidationFail));

            System.Xml.XmlReaderSettings readerSettings_Ret = new System.Xml.XmlReaderSettings();
            readerSettings_Ret.ValidationType = System.Xml.ValidationType.Schema;
            readerSettings_Ret.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(OnValidationFail);
            readerSettings_Ret.Schemas.Add(schema);

            return readerSettings_Ret;
        }
    }


    private void OnValidationFail(object s, System.Xml.Schema.ValidationEventArgs e)
    {
        throw new OperationCanceledException("Validation error: " + e.Message);
    }

    [TestMethod]
    public void XSD_Valid_Test()
    {
         // Valid elements and valid namespace
        String sXML_Valid = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<Root xmlns=\"http://mynamespace\"><Child /></Root>";
        System.Xml.XmlReader xmlReader_Valid = 
            System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_Valid), this.ReaderSettings);
        while (xmlReader_Valid.Read()) { } // no fail expected
    }

    [TestMethod]
    [ExpectedException(typeof(OperationCanceledException))]
    public void XSD_NotValid_1()
    {
         // No valid elements, while valid namespace
        String sXML_NotValid_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<BadRoot xmlns=\"http://mynamespace\"><Child /></BadRoot>";
        System.Xml.XmlReader xmlReader_NoValid_1 = 
            System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_NotValid_1), this.ReaderSettings);
        while (xmlReader_NoValid_1.Read()) ;
    }

    [TestMethod]
    [ExpectedException(typeof(OperationCanceledException))]
    public void XSD_NotValid_2()
    {
        // No valid elements and no valid namespace
        String sXML_NotValid_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<Root xmlns=\"http://Wrongnamespace\"><NotValidChild /></Root>";
        System.Xml.XmlReader xmlReader_NoValid_2 = 
            System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_NotValid_2), this.ReaderSettings);
        while (xmlReader_NoValid_2.Read()) ;
    }
}

Is it normal behavior? How to force correct namespace targeting?
And also how to force Root element to be required in the case XSD has additional Root2 element?

  • 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-09T22:15:11+00:00Added an answer on June 9, 2026 at 10:15 pm

    If you enable schema validation warnings, you’ll get the following error:

    Could not find schema information for the element ‘http://Wrongnamespace:Root’.

    Use

    readerSettings_Ret.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
    

    A couple of small things:

    1. Even in a unit test, you shouldn’t be creating XML by string manipulation. Always use XML APIs to create XML, as they know “all the rules”.
    2. Even in a unit test, you should place your creation of objects that implement IDisposable in using blocks. In fact, I’d say especially in a unit test, where each test should be independent of the other tests, you want to make sure that one test has cleaned up before starting the next.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that looks like this: <Data xmlns=http://www.domain.com/schema/data xmlns:dmd=http://www.domain.com/schema/data-metadata > <Something>...</Something>
I have the following start of an XSD: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns:no=http://www.sychophants.com>
I have XML something like this: <?xml version=1.0?> <a xmlns=http://mynamespace> <b> <c val=test />
I want to manipulate xml doc having default namespace but no prefix. Is there
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
I am having an XML string like <?xml version=1.0?> <FullServiceAddressCorrectionDelivery xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <AuthenticationInfo xmlns=http://www.usps.com/postalone/services/UserAuthenticationSchema>
I am having trouble validating my xml schema. I get these errors on the
Having this (simplified) XML: <?xml version=1.0 encoding=UTF-8?> <kml> <Document> <Placemark> <name>Poly 1</name> <Polygon> <coordinates>
having the following XML sample document, I need to issue an XPath/XQuery expression to
I seem to be having problems doing some basic parsing of an XML document

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.