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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:33:02+00:00 2026-05-31T16:33:02+00:00

I’m trying to read the data from an XML file, validating it against the

  • 0

I’m trying to read the data from an XML file, validating it against the XSD it suggests, into a single data structure (such as XmlDocument). I have a solution, but it requires 2 passes through the file, and I’m wondering if there’s a single-pass solution.

MyBooks.xml:

<Books xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:noNamespaceSchemaLocation='books.xsd' id='999'>
    <Book>Book A</Book>
    <Book>Book B</Book>
</Books>

Books.xsd:

<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
    elementFormDefault='qualified'
    attributeFormDefault='unqualified'>
    <xs:element name='Books'>
        <xs:complexType>
            <xs:sequence>
                <xs:element name='Book' type='xs:string' />
            </xs:sequence>
            <xs:attribute name='id' type='xs:unsignedShort' use='required' />
        </xs:complexType>
    </xs:element>
</xs:schema>

Let’s say MyBooks.xml and Books.xsd are in the same directory.

Validate:

//Given a filename pointing to the XML file
var settings = new XmlReaderSettings();

settings.ValidationType = ValidationType.Schema;

settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

settings.CloseInput = true;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCB);
//eg:
//private static void ValidationCB(object sender, ValidationEventArgs args)
//{ throw new ApplicationException(args.Message); }

using(var reader = XmlReader.Create(filename, settings))
{ while(reader.Read()) ; }

Read into XmlDocument:

XmlDocument x = new XmlDocument();
x.Load(filename);

Sure, I could collect the nodes as the read from the XmlReader is taking place, but I’d rather not have to do it myself, if possible. Any suggestion?

Thanks in advance

  • 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-31T16:33:03+00:00Added an answer on May 31, 2026 at 4:33 pm

    You’re very close with your solution; what you need to do is to use a validating reader to load your XML; this way the validation is done with your loading, in one pass; validation errors will not stop you from loading the document.

    These are the high level steps that I usually use with a ValidateXml helper function; it all starts with a compiled XmlSchemaSet:

    public bool ValidateXml(XmlSchemaSet xset)
    

    I set the reader settings (which you did, too):

    XmlReaderSettings settings = new XmlReaderSettings { ValidationType = ValidationType.Schema, Schemas = xset, ConformanceLevel = ConformanceLevel.Document };
    settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
    // Use your helper class that collects validation events. 
    XsdUtils.Utils.SmartValidationHandler svh = new XsdUtils.Utils.SmartValidationHandler(Paschi.Xml.DefaultResolver.Instance);
    settings.ValidationEventHandler += svh.ValidationCallbackOne;
    

    Then I get a reader:

    XmlReader xvr = XmlReader.Create(filename, settings);
    

    Then I read the file, which brings the validation in:

    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(xvr);
    

    Your validation handler has the results now; one thing I also do is to ensure that the document element that was loaded, actually has a corresponding global element definition in the xml schema set.

    XmlQualifiedName qn = XmlQualifiedName.Empty;
    if (xdoc.DocumentElement != null)
    {
            if (string.IsNullOrEmpty(xdoc.DocumentElement.NamespaceURI))
            {
                  qn = new XmlQualifiedName(xdoc.DocumentElement.LocalName);
            }
            else
            {
                   qn = new XmlQualifiedName(xdoc.DocumentElement.LocalName, xdoc.DocumentElement.NamespaceURI);
             }
    }
    return !(svh.HasError || qn.IsEmpty || (!xset.GlobalElements.Contains(qn)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to understand how to use SyndicationItem to display feed which is

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.