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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:57:20+00:00 2026-05-28T00:57:20+00:00

Q: I want to break the loop if some exception has been caught in

  • 0

Q:

I want to break the loop if some exception has been caught in the event.

The code :

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

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += Handler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null,System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;

using (StreamReader str_reader = new StreamReader(targetFileName))
{

    using (XmlReader validatingReader = XmlReader.Create(str_reader, settings))
    {
        while (validatingReader.Read())
        {
            //I wanna to break this loop if there is some caught error .
        }
    }
}

private static void Handler(object sender, ValidationEventArgs e)
{

    if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
    {
        try
        {
            throw new Exception(
                 String.Format("Line: {0}, Position: {1} \"{2}\"",
                     e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
        }
        catch (Exception ee)
        {
            Common.ErrMappingForInformix.WriteLog(ee.Message);
            ScheduleForm sf = new ScheduleForm();
            sf.ShowStatus("Error,Invalid xml file", "error", "", 1);

        }

    }

}

# Edit 1 :

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

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += (senderValidation, ee) =>
{
    if (ee.Severity == XmlSeverityType.Error || ee.Severity == XmlSeverityType.Warning)
    {
        try
        {
            this.validationFailed = true;
            throw new Exception(
                 String.Format("Line: {0}, Position: {1} \"{2}\"",
                     ee.Exception.LineNumber, ee.Exception.LinePosition, ee.Exception.Message));
        }
        catch (Exception ex)
        {

            Common.ErrMappingForInformix.WriteLog(ex.Message);
            this.ShowStatus("Error", "error", "", 1);
        }

    }
};

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;

using (StreamReader str_reader = new StreamReader(targetFileName))
{

    using (XmlReader validatingReader = XmlReader.Create(str_reader, settings))
    {
        while (validatingReader.Read())
        {
            //Loop through the document
            if (validationFailed)
            {
                break;
            }

        }
    }
}

It breaks the loop but it doesn’t execute the event handler.

  • 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-28T00:57:21+00:00Added an answer on May 28, 2026 at 12:57 am

    You could set a flag to indicate there’s an error in the Handler method, then detect that flag and use break; if it’s set.

    In this case, it might be better to use an inline anonymous delegate for the validation handler, so you don’t need to add a new member variable to the class containing the methods. (Adding a member variable isn’t so bad if the class only deals with XML reading/validation)

    Edit: anonymous inline delegate means you can add an event handler that doesn’t have to be in a separate method, basically. For example:

    bool validationFailed = false; // outside of the handler
    settings.ValidationEventHandler += (sender, e) =>
    {
        if (e.Severity == XmlS....
        // set validationFailed = true; in here to signal failure
    };
    
    // then detect validationFailed being true to know if/when to break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am preparing some code: for(int a = 1; a <= 100; a++) //loop
Javascript loop continue,or break to display? This is my code use for show or
In the code of my game, I want to remove some elements from a
some Function() { for(xxxxxx){ //a infinite loop or just a long code loading an
What if I have nested loops, and I want to break out of all
I'm trying to use the CSS word-wrap property with break-word value. I want to
I have a service running some different tasks in a loop until the service
For some months I've been working on a home-made operating system. Currently, it boots
I need some help with the code below. What I'm trying to do is
I have some troubles to find the right loop to check if some values

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.