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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:42+00:00 2026-05-28T07:43:42+00:00

I have modified the MSDN example to reflect my problem. When using a namespace

  • 0

I have modified the MSDN example to reflect my problem.

When using a namespace I can’t get the document to validate as I would expect and when validating a document that doesnt have a namespace it validates regardless of whether or not it has an error in it or not.

Dim errors As Boolean = False

Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("{0}", e.Message)
    errors = True
End Sub

Private Function AddNameSpace(ByVal xDoc As XDocument, ByVal ns As XNamespace) As XDocument
    For Each element As XElement In xDoc.Descendants
        element.Name = ns + element.Name.LocalName
    Next
    Return xDoc
End Function

Sub Main()
    Dim xsdMarkup As XElement = _
        <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns="http://somenamespace.com" targetNamespace="http://somenamespace.com">
            <xsd:element name='Root'>
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
                        <xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
    Dim schemas As XmlSchemaSet = New XmlSchemaSet()
    schemas.Add("http://somenamespace.com", xsdMarkup.CreateReader)

    Dim doc1 As XDocument = _
        <?xml version='1.0'?>
        <Root>
            <Child1>content1</Child1>
            <Child2>content1</Child2>
        </Root>

    Dim doc2 As XDocument = _
        <?xml version='1.0'?>
        <Root>
            <Child1>content1</Child1>
            <Child3>content1</Child3>
        </Root>

    Dim ns As XNamespace = "http://somenamespace.com"
    doc1 = AddNameSpace(doc1, ns)

    Console.WriteLine("Validating doc1")
    errors = False
    doc1.Validate(schemas, AddressOf XSDErrors)
    Console.WriteLine("doc1 {0}", IIf(errors = True, "did not validate", "validated"))

    Console.WriteLine()
    Console.WriteLine("Validating doc2")
    errors = False
    doc2.Validate(schemas, AddressOf XSDErrors)
    Console.WriteLine("doc2 {0}", IIf(errors = True, "did not validate", "validated"))

End Sub

Output:

Validating doc1

The element ‘Root’ in namespace ‘http://somenamespace.com&#8217; has invalid child element ‘Child1’ in namespace ‘http://somenamespace.com&#8217;. List of possible elements expected: ‘Child1’.

doc1 did not validate

Validating doc2

doc2 validated

  • 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-28T07:43:42+00:00Added an answer on May 28, 2026 at 7:43 am

    Well you will need to add elementFormDefault="qualified" to your schema (on the xsd:schema element) if you want your doc1 where you put the namespace on each element to be valid. With your current schema a valid instance would be one where the Root is in the targetNamespace but the ChildX elements are in no namespace.

    The second issue is a known problem with schema validation and namespaces, the validating parser looks for a matching schema for the root element, if there is none that it does lax validation so you don’t get a validation error. With the XmlReader API you can ask for warning to be emitted in that case but I don’t know how to do that with the Validate method. So you would need code like

    Imports System
    Imports System.Xml
    Imports System.Xml.Linq
    Imports System.Xml.Schema
    
    Module Module1
    
        Dim errors As Boolean = False
    
        Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
            Console.WriteLine("{0}", e.Message)
            errors = True
        End Sub
    
        Private Function AddNameSpace(ByVal xDoc As XDocument, ByVal ns As XNamespace) As XDocument
            For Each element As XElement In xDoc.Descendants
                element.Name = ns + element.Name.LocalName
            Next
            Return xDoc
        End Function
    
        Sub Main()
            Dim xsdMarkup As XElement = _
                <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns="http://somenamespace.com" targetNamespace="http://somenamespace.com" elementFormDefault="qualified">
                    <xsd:element name='Root'>
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
                                <xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                </xsd:schema>
            Dim schemas As XmlSchemaSet = New XmlSchemaSet()
            schemas.Add("http://somenamespace.com", xsdMarkup.CreateReader)
    
            Dim doc1 As XDocument = _
                <?xml version='1.0'?>
                <Root>
                    <Child1>content1</Child1>
                    <Child2>content1</Child2>
                </Root>
    
            Dim doc2 As XDocument = _
                <?xml version='1.0'?>
                <Root>
                    <Child1>content1</Child1>
                    <Child3>content1</Child3>
                </Root>
    
            Dim ns As XNamespace = "http://somenamespace.com"
            doc1 = AddNameSpace(doc1, ns)
    
            Console.WriteLine("Validating doc1")
            errors = False
            doc1.Validate(schemas, AddressOf XSDErrors)
            Console.WriteLine("doc1 {0}", IIf(errors = True, "did not validate", "validated"))
    
            Console.WriteLine()
            Console.WriteLine("Validating doc2")
            Dim xrs As New XmlReaderSettings()
            xrs.ValidationType = ValidationType.Schema
            xrs.ValidationFlags = xrs.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
            xrs.Schemas = schemas
            AddHandler xrs.ValidationEventHandler, AddressOf XSDErrors
            errors = False
            Using xr1 As XmlReader = doc2.CreateReader()
                Using xr2 As XmlReader = XmlReader.Create(xr1, xrs)
                    While xr2.Read()
    
                    End While
                End Using
            End Using
            Console.WriteLine("doc2 {0}", IIf(errors = True, "did not validate", "validated"))
    
        End Sub
    
    End Module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have modified my treeview to look like an Org chart using the example
I have a dataset that I have modified into an xml document and then
I have an XML object (loaded using XMLHTTPRequest 's responseXML ). I have modified
I am using DotNetOpenAuth in my ASP.Net Website. I have modified it to work
I have the following code, which is a modified version from MSDN's website, to
I have modified my etc/hosts file (under Windows 7), so that www.example.com and demo.example.com
I have modified the example found here to use two io channels. None of
I have a gridview and have modified it following this article: http://msdn.microsoft.com/en-us/library/aa992036.aspx#Y3473 to allow
I'm using the following code (which is a sample from the MSDN slightly modified)
I have modified my wpf button but the problem is when i hit 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.