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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:50:44+00:00 2026-05-20T18:50:44+00:00

I have a Class Library project which houses some shared code between other projects

  • 0

I have a Class Library project which houses some shared code between other projects in my solution. One of these pieces of shared code involves running an XML validation against an XSD file. The name of the XSD is passed as a parameter to the method and then loaded using Assembly.GetFile().

The problem is that the XSD file imports two other XSDs. I’ve loaded all three as Resources within my Class Library but from what I’ve read the xsd:import is not going to work. Is there an alternative approach to making these XSDs available within my Class Library Project without breaking the xsd:import statements?

Edit – Update

I implemented Alexander’s suggestion below but as I stated in my comment, whenever GetEntity() is called for an xs:import‘d XSD, ofObjectToReturn is null. This caused the first instance of an xs:import‘d type to throw an exception “type not defined.”

In an effort to resolve this issue I altered GetEntity() to return GetManifestResourceStream() regardless of ofObjectToReturn‘s value. This now seems to work for the first level of xs:import statements but a secondary xs:import inside one of the original xs:import XSDs is not working. I’ve confirmed that GetEntity() is being called for this secondary xs:import but I’m receiving the “type not defined” exception for a type defined within this secondary XSD.

  • TopLevel.xsd – types resolve fine
    • FirstLevelImport1.xsd – types resolve fine
    • FirstLevelImport2.xsd – types resolve fine
      • SecondLevelImport1.xsd – “type not defined” exception thrown for type defined in this XSD

The “type not defined” exception is thrown during XmlReader.Create() that is passed the XmlReaderSettings defining the schema validation.

  • 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-20T18:50:45+00:00Added an answer on May 20, 2026 at 6:50 pm

    To resolve the files, which are added by either xsd:import or xsd:include you can use a custom XmlResolver. You can find an example of an ResourceXmlResolver below. It assumes, that the assembly’s name is “AYez.EmbeddedXsdTests“.

    using System.Xml;
    using System.Xml.Schema;
    using NUnit.Framework;
    
    namespace AYez.EmbeddedXsdTests
    {
        [TestFixture]
        public class EmbeddedXsdTests
        {
            [Test]
            public void SomeEntryPoint()
            {
                var schemaSet = new XmlSchemaSet {XmlResolver = new ResourceXmlResolver()};
                schemaSet.Add("rrn:org.xcbl:schemas/xcbl/v4_0/financial/v1_0/financial.xsd", @"Invoice.xsd");
                schemaSet.Compile();
    
                var settings = new XmlReaderSettings { ValidationType = ValidationType.Schema, Schemas = schemaSet };
    
                settings.ValidationEventHandler += delegate(object o, ValidationEventArgs e)
                {
                    switch (e.Severity)
                    {
                        case XmlSeverityType.Error:
                            Console.Write("Error: {0}", e.Message);
                            break;
                        case XmlSeverityType.Warning:
                            Console.Write("Warning: {0}", e.Message);
                            break;
                    }
                };
                var xmlReader = XmlReader.Create(@"d:\temp\Invoice.xml", settings);
                while (xmlReader.Read()) { /*TODO: Nothing*/} // Validation is performed while reading
    
            }
        }
    
        public class ResourceXmlResolver: XmlResolver
        {
            /// <summary>
            /// When overridden in a derived class, maps a URI to an object containing the actual resource.
            /// </summary>
            /// <returns>
            /// A System.IO.Stream object or null if a type other than stream is specified.
            /// </returns>
            /// <param name="absoluteUri">The URI returned from <see cref="M:System.Xml.XmlResolver.ResolveUri(System.Uri,System.String)"/>. </param><param name="role">The current version does not use this parameter when resolving URIs. This is provided for future extensibility purposes. For example, this can be mapped to the xlink:role and used as an implementation specific argument in other scenarios. </param><param name="ofObjectToReturn">The type of object to return. The current version only returns System.IO.Stream objects. </param><exception cref="T:System.Xml.XmlException"><paramref name="ofObjectToReturn"/> is not a Stream type. </exception><exception cref="T:System.UriFormatException">The specified URI is not an absolute URI. </exception><exception cref="T:System.ArgumentNullException"><paramref name="absoluteUri"/> is null. </exception><exception cref="T:System.Exception">There is a runtime error (for example, an interrupted server connection). </exception>
            public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
            {   
                    // If ofObjectToReturn is null, then any of the following types can be returned for correct processing:
                    // Stream, TextReader, XmlReader or descendants of XmlSchema
                    var result =  this.GetType().Assembly.GetManifestResourceStream(string.Format("AYez.EmbeddedXsdTests.{0}",
                                                                                                 Path.GetFileName(absoluteUri.ToString())));                
                    // set a conditional breakpoint "result==null" here
                    return result;
            }
    
            /// <summary>
            /// When overridden in a derived class, sets the credentials used to authenticate Web requests.
            /// </summary>
            /// <returns>
            /// An <see cref="T:System.Net.ICredentials"/> object. If this property is not set, the value defaults to null; that is, the XmlResolver has no user credentials.
            /// </returns>
            public override ICredentials Credentials
            {
                set { throw new NotImplementedException(); }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an solution in VS 2008 which contains two class library projects and
I have a class library project which contains some content files configured with the
I have created a class library in VB .NET. Some code in the library
I have a class library project which uses a namespace (e.g., Cosmos.Creator.Util). I then
I have a C# class library and a startup project (a console app). The
I have a Windows Service project, A, with a dependency on a class library
I have a class library with some extension methods written in C# and an
In the past I had quite some reusable code in my project which I
I have a class/library project I made in Visual Studio, a Spreadsheet in the
I'm making a new Project which is a class library. my problem is I

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.