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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:42:47+00:00 2026-06-01T07:42:47+00:00

I am working with multiple XElement objects to provide some user-specified data for several

  • 0

I am working with multiple XElement objects to provide some user-specified data for several objects in my library. I’m trying to avoid specifying the structure of the entire XML file because the library shouldn’t care what the entire XML looks like as long as the specific elements it needs are structured correctly.

To that end, I have 3 separate XSD files that define schema for each of the 3 XElements my class needs, however I am running into some problems validating the XElement against the schema. There doesn’t appear to be a method to do that without a workaround.

From the MSDN page, the XElement.Validate() extension method appears to be geared to re-validating sub-elements of a larger file. The XmlSchemaObject argument is causing my problems as I cannot assume it will be present in any of the XElements. I think I can work around this issue by grabbing the XmlSchemaElement from my XmlSchemaSet to pass as the XmlSchemaObject argument, but since the XmlSchemaSet already defines everything, it seems strange to have to do that.

Is there a better option for validating an XElement with a schema without first validating the entire XDocument?

Or should i just let the business layer handle the schema validation in the application and let the library assume the XElement is properly formed (I considered this option, but as a personal preference prefer to avoid throwing exceptions and would rather just let the calling method know that the XElement is invalid via a return parameter).

  • 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-01T07:42:48+00:00Added an answer on June 1, 2026 at 7:42 am

    I understand the problem you have with the provided API, as far as I see it you have two options, one being to put your XElement into an XDocument with e.g. XDocument doc = new XDocument(xElementToValidate); and then to call the Validate method on that XDocument where all you have to pass in is the XmlSchemaSet, the second option being the way you outlined yourself, namely to use the Validate method of the XElement, making sure you pass in the XmlSchemaSet and the root element definition in that schema set as the XmlSchemaObject. If these are simple schemas with just one top level element definition all you have to do is e.g.

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add(null, "schema.xsd");
        schemaSet.Compile();
    
        XmlSchemaObject schemaObject = schemaSet.GlobalElements.Values.OfType<XmlSchemaObject>().First();
    

    If you wrap one of the two approaches into a method then it shouldn’t be any more effort than calling a similar convenience method the .NET framework could have supplied.

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

Sidebar

Related Questions

I've seen several other posts similar to this (namely https://stackoverflow.com/questions/5237/solutions-for-working-with-multiple-branches-in-asp-net ) but there are
im trying to get role provider working in multiple environments and hitting a wall
Is there a more elegant way of working with multiple objects within an object.
If I am working in multiple branches on a single feature, I use git
I'm working with multiple images that need to be compressed using the DXT/S3TC format
I'm currently working on multiple changes/bugs and I would like to be able to
I'm rather new to working with multiple threads in a database (most of my
how can i make java program working with multiple languages (frensh, english , arabic
I'm very interesting in inexpensive (better free) SQL query tool for working with multiple
This is a very basic question, but my brain isn't working and multiple attempts

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.